How can I replace Japanese texts between with their corresponding English texts? I have a hundreds of AliasName that I need to replace. Since I am new to python and XML, a step by step instruction would be greatly appreciated.
Here is how my xml file looks like:
<Workspace>
<Dataset>
<DataElement>
<Name>POP</Name>
<Fields>
<Field>
<Name>State</Name>
<AliasName>?</AliasName> # replace to <AliasName>State</AliasName>
</Field>
<Field>
<Name>City</Name>
<AliasName>??</AliasName> # replace to <AliasName>City</AliasName>
</Field>
(...)
Here is how far I have came. I created a dictionary, hoping that I can use it for replace(?)
>>> x = ("State", "州", "City", "都市", "Town", "町")
>>> dic = dict(x[n:n+2] for n in xrange(0, len(x), 2))
>>> print dic # Japanese appears strange in python GUI
{'Town': '\x92\xac', 'City':'\x93s\x8es', 'State': '\x8fB'}
I also managed to select and display all Japanese texts between <AliasName></AliasName>.
from xml.dom import minidom
xdoc = minidom.parse(r"D:\Desktop\python\src\sample.xml")
workspace = xdoc.getElementsByTagName("AliasName")
for i, element in enumerate(workspace):
print (i, element.childNodes[0].data)
Fom here I am stuck. How can I replace and save updates on xml?