python - Make sentences with 1st|2nd|3rd|4th to XML according to a special specification -
i want make sentences xml
i meet @ 1st. 5th... ok, 5th? today 2nd\n aug.3rd
like this:
<text valign="top" vposition="85.00"> meet @ 1<font script="super">st</font>. </text> <text valign="top" vposition="85.00"> 5<font script="super">th</font>... ok, 5<font script="super">th</font> </text> <text valign="top" vposition="85.00"> today 2<font script="super">nd</font>\n </text> <text valign="top" vposition="85.00"> aug.3<font script="super">rd</font>\n </text>
i using minidom, after many posts , answers, don't mind rewrite code other parser. @ beginning, thought easy, replace st|nd|rd|th
with
<font script="super">st|nd|rd|th</font>
, createtextnode() new string.
however, sign <, > , "
turn out < > , $quot;
writexml() method. works xml specification, not read.
how can it? much.
here's can xml.etree.elementtree standard library:
import re import xml.etree.elementtree et data = """i meet @ 1st. 5th... ok, 5th? today 2nd aug.3rd""" endings = ['st', 'th', 'nd', 'rd'] pattern = re.compile('(%s)' % "|".join(endings)) root = et.element('root') line in data.split('\n'): items = [] item in re.split(pattern, line): if item in endings: items.append('<font script="super">%s</font>' % item) else: items.append(item) element = et.fromstring("""<text valign="top" vposition="85.00">%s</text>""" % ''.join(items)) root.append(element) print et.tostring(root)
it produces following xml:
<root> <text valign="top" vposition="85.00">i meet @ 1<font script="super">st</font>. </text> <text valign="top" vposition="85.00">5<font script="super">th</font>... ok, 5<font script="super">th</font>? </text> <text valign="top" vposition="85.00">today 2 <font script="super">nd</font> </text> <text valign="top" vposition="85.00">aug.3 <font script="super">rd</font> </text> </root>
Comments
Post a Comment