Split string to desired form using Python -
i have data in following form:
<a> <b> _:h1 <c>. _:h1 <e> "200"^^<http://www.w3.org/2001/xmlschema#integer> <f> . _:h1 <date> "mon, 30 apr 2012 07:01:51 gmt" <p> . _:h1 <server> "apache/2" <df> . _:h1 <last-modified> "sun, 25 mar 2012 14:15:37 gmt" <hf> .
i need convert following form using python:
<a> <b> _:h1. <1> <c>. _:h1 <e> "200"^^<http://www.w3.org/2001/xmlschema#integer> . <1> <f>. _:h1 <date> "mon, 30 apr 2012 07:01:51 gmt". <1> <p>. _:h1 <server> "apache/2" . <1> <df>. _:h1 <last-modified> "sun, 25 mar 2012 14:15:37 gmt" . <1> <hf>.
i wrote code in python using str.split()
method. splits string based on space. however, not solve purpose using "sun, 25 mar 2012 14:15:37 gmt" gets split. there other way achieve using python?
you can use rfind
or rindex
methods find last occurrence of <
in lines.
data = """[your data]""" data_new = "" line in data.splitlines(): = line.rfind("<") data_new += line if == -1 else line[:i] + ". \n<1> " + line[i:] + "\n" data_new = data_new.strip()
Comments
Post a Comment