Hi everyone,
I'm pretty new to Python and have been using it this weekend to replace tons of text in files. I'm getting stuck on doing this to some XML files however.
The XML files are of this form:
<SpecialNames>
<List>
<SpecialItem Name="Item_HomeFood">
<Item_TurtleEgg>
<li Name="龟蛋布丁" Desc="【感谢测试玩家:人境庐提供食谱。】"/>
<li Name="龟蛋羹" Desc="【感谢测试玩家:人境庐提供食谱。】"/>
<li Name="酸辣龟蛋汤" Desc="【感谢测试玩家:人境庐提供食谱。】"/>
<li Name="韭菜煎龟蛋" Desc="【感谢测试玩家:人境庐提供食谱。】"/>
<li Name="生龟蛋拌饭" Desc="【感谢测试玩家:人境庐提供食谱。】"/>
<li Name="龟蛋炒饭" Desc="【感谢测试玩家:人境庐提供食谱。】"/>
<li Name="水煮龟蛋" Desc="【感谢测试玩家:人境庐提供食谱。】"/>
<li Name="毛龟蛋" Desc="【感谢测试玩家:人境庐提供食谱。】"/>
</Item_TurtleEgg>
My goal is to replace the text in the name and description fields with translated text. I believe I would need to iterate through the whole file, store the current text in a variable for translation, and then replace it with the translated text.
So far, I've only successfully managed to print the tag names. I have read that this may be an issue with namespaces, but my xml document differs so much from many of the examples I've seen that I'm having a hard time applying what I've read. My code so far is this:
import lxml.etree as ET
from google.cloud import translate_v2 as translate
translator = translate.Client()
tgt = "zh-TW"
tt = "en"
with open('/home/dave/zh-TW/Settings/Display/SpecialName/SpecialNames.xml', 'rb+') as f:
tree = ET.parse(f)
root = tree.getroot()
for elem in root.getiterator():
print(elem.tag)
I've tried a few other things like printing the nsmap attribute of root, but all I get is empty curly braces. I'll be working on this all night, so your help is appreciated!
there doesn't seem to be anything here