如何用相应的英文文本替换日文文本?我有几百个需要替换的AliasName。由于我是python和XML的新手,因此非常感谢一步一步的指导。
以下是我的xml文件的样子:
<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>
(...)
这是我走了多远。我创建了一个字典,希望我能用它替换(?)
>>> 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'}
我还设法在<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)
我在这里被困住了。如何在xml上替换和保存更新?
答案 0 :(得分:2)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import xml.etree.cElementTree as etree
en2jp = {u"State": u"州", u"City": u"都市", u"Town": u"町"} # English -> Japanese
tree = etree.parse(filename)
for field in tree.iterfind('.//Fields/Field'):
name, alias = [field.find(tag) for tag in ['Name', 'AliasName']]
if name is None or name.text is None or alias is None: continue
alias.text = en2jp.get(name.text.strip(), alias.text)
tree.write('output.xml', encoding='utf-8')
您不需要使用字典将<AliasName>
的文字设置为与同一<Name>
中<Field>
标记的文字相同:
#!/usr/bin/env python
import xml.etree.cElementTree as etree
tree = etree.parse("input.xml")
for field in tree.iterfind('.//Fields/FieldArray/Field'):
name, alias = [field.find(tag) for tag in ['Name', 'AliasName']]
if name is None or alias is None:
continue # skip if either <Name> or <AliasName> are not found
alias.text = name.text # set <AliasName> text to be the same as <Name>
tree.write('output.xml', encoding='utf-8')
答案 1 :(得分:0)
使用string.replace()函数怎么样?
string.replace(str,old,new [,maxreplace]) 返回字符串str的副本,其中所有出现的substring old都替换为new。如果给出了可选参数maxreplace,则替换第一个maxreplace事件。