使用 elementtree 和 python 根据 xml 上的标签名称替换属性
Replacing attributes according to tag name on xml using elementtree and python
我有xml个文件
<?xml version="1.0"?>
<data>
<country name="Panama">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Malaysia">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Liechtenstein">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
我需要找到所有国家/地区标签,检查文本是否与我的国家/地区列表的当前位置相同,如果它们不相同,我们会用我列表中的正确名称替换国家/地区名称。它还应该创建一个 log.txt 文件(这部分没问题)。例如,有些名字顺序不对(巴拿马的邻居不是奥地利和瑞士)所以他们需要替换,这是一个很长的 xml 所以我想写一个脚本来自动完成。
import xml.etree.ElementTree as ET
import os
from xml.etree.ElementTree import SubElement
base_path = os.path.dirname(os.path.realpath(__file__))
xml_file = os.path.join(base_path, 'data.xml')
tree = ET.parse(xml_file)
root = tree.getroot()
Tags = ['country', 'rank', 'year']
right_data = ['Liechtenstein', 'Singapore', 'Panama']
# I need a log of the changes
f = open('Log.txt','w')
i =0
for tag in Tags[i:]:
print tag
for child in root.iter():
print child
if tag == child.tag:
print 'We found matching tag %s' % tag
if child.text != right_data[i]:
print 'We are changing %s ' % child.text, 'to --> %s'% right_data[i]
f.write('Changing %s -->' % child.text)
f.write('to name %s\n' % right_data[i])
#This is where the problems start
#This is supposed to find text attribute and replace it the right_data[i] at position i
#I get this error when I run my program
#SyntaxError: can't assign to function call
tree.find(child.text) = right_data[i]
else:
"There is no such tag"
f.close()
new_data = ET.tostring(root)
new_xml = open('dataUpdated.xml', 'w')
new_xml.write(new_data)
我知道我可以用这种方式替换 xml 文件中的文本。
tree.find('Panama').text = 'Liechtenstein'
tree.write(datafile)
然而,当我将一个列表(righ_data[] 和 child.text)作为参数传递时,它不喜欢它,它给了我上述错误。
我停止使用 find() 方法。请参阅下文,了解我是如何解决问题的。 Key 和 val 是我的字典。
customDict = {'Soap':'Dial', 'Shampoo': 'H&S'}
for child in root.iter():
for key, val customDict.items():
if child.tag == key:
child.tex = val
这将找到标签,检查它是否是正确的标签并相应地修改它。
我有xml个文件
<?xml version="1.0"?>
<data>
<country name="Panama">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Malaysia">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Liechtenstein">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
我需要找到所有国家/地区标签,检查文本是否与我的国家/地区列表的当前位置相同,如果它们不相同,我们会用我列表中的正确名称替换国家/地区名称。它还应该创建一个 log.txt 文件(这部分没问题)。例如,有些名字顺序不对(巴拿马的邻居不是奥地利和瑞士)所以他们需要替换,这是一个很长的 xml 所以我想写一个脚本来自动完成。
import xml.etree.ElementTree as ET
import os
from xml.etree.ElementTree import SubElement
base_path = os.path.dirname(os.path.realpath(__file__))
xml_file = os.path.join(base_path, 'data.xml')
tree = ET.parse(xml_file)
root = tree.getroot()
Tags = ['country', 'rank', 'year']
right_data = ['Liechtenstein', 'Singapore', 'Panama']
# I need a log of the changes
f = open('Log.txt','w')
i =0
for tag in Tags[i:]:
print tag
for child in root.iter():
print child
if tag == child.tag:
print 'We found matching tag %s' % tag
if child.text != right_data[i]:
print 'We are changing %s ' % child.text, 'to --> %s'% right_data[i]
f.write('Changing %s -->' % child.text)
f.write('to name %s\n' % right_data[i])
#This is where the problems start
#This is supposed to find text attribute and replace it the right_data[i] at position i
#I get this error when I run my program
#SyntaxError: can't assign to function call
tree.find(child.text) = right_data[i]
else:
"There is no such tag"
f.close()
new_data = ET.tostring(root)
new_xml = open('dataUpdated.xml', 'w')
new_xml.write(new_data)
我知道我可以用这种方式替换 xml 文件中的文本。
tree.find('Panama').text = 'Liechtenstein'
tree.write(datafile)
然而,当我将一个列表(righ_data[] 和 child.text)作为参数传递时,它不喜欢它,它给了我上述错误。
我停止使用 find() 方法。请参阅下文,了解我是如何解决问题的。 Key 和 val 是我的字典。
customDict = {'Soap':'Dial', 'Shampoo': 'H&S'}
for child in root.iter():
for key, val customDict.items():
if child.tag == key:
child.tex = val
这将找到标签,检查它是否是正确的标签并相应地修改它。