
[英]Replacing elements containing escape characters / in an XML file using python
[英]Python: replacing xml elements with multiprocessing
我有两个不同的 .xml 文件,我想创建一个程序,用 2.xml 的孩子替换 1.xml 的孩子
1.xml 看起来像这样:
<CATALOG>
<ITEM>
<COLOR>11</COLOR>
<COLORNAME>Black</COLORNAME>
</ITEM>
<ITEM>
<COLOR>41</COLOR>
<COLORNAME>Aqua</COLORNAME>
</ITEM>
<ITEM>
...
</ITEM>
</CATALOG>
2.xml 看起来像这样:
<CODES>
<ITEM>
<ITEMTYPE>P</ITEMTYPE>
<ITEMID>44</ITEMID>
<COLOR>Black</COLOR>
</ITEM>
<ITEM>
<ITEMTYPE>P</ITEMTYPE>
<ITEMID>44</ITEMID>
<COLOR>Blue</COLOR>
</ITEM>
<ITEM>
...
</ITEM>
</CODES>
我的目标是获得一个 3.xml 文件,它应该如下所示:
<CODES>
<ITEM>
<ITEMTYPE>P</ITEMTYPE>
<ITEMID>44</ITEMID>
<COLOR>11</COLOR>
</ITEM>
<ITEM>
<ITEMTYPE>P</ITEMTYPE>
<ITEMID>44</ITEMID>
<COLOR>7</COLOR>
</ITEM>
<ITEM>
...
</ITEM>
</CODES>
我现在编写了一个脚本,将 1.xml 的 < <COLOR>
更改为 2.xml 的<COLOR>
,并将 2.xml 的<COLORNAME>
与 1.xml 的<COLOR>
进行比较
import xml.etree.ElementTree as ET
codes = ET.parse('./datasets/1.xml')
root_codes = codes.getroot()
colors = ET.parse('./datasets/2.xml')
root_colors = colors.getroot()
done=0
for colorname in root_codes.findall('ITEM'):
codes_color_name = colorname.find('COLOR').text
for color in root_colors.iter('ITEM'):
color_name = color.find('COLORNAME').text
if codes_color_name == color_name:
color_id = color.find('COLOR').text
colorname.find('COLOR').text = str(color_id)
codes.write('3.xml')
done=done+1
print(done)
我用 1.xml 的缩减版本尝试了这个,它似乎有效。 因为这真的很慢(1.xml ~300k 行和 2.xml ~3k 行)我尝试使用多处理,这就是我想出的:
import xml.etree.ElementTree as ET
from multiprocessing import Pool
import multiprocessing
import os
codes = ET.parse('./datasets/1.xml')
root_codes = codes.getroot()
colors = ET.parse('./datasets/2.xml')
root_colors = colors.getroot()
def process_item(colorname):
codes_color_name = colorname.find('COLOR').text
for color in root_colors.iter('ITEM'):
color_name = color.find('COLORNAME').text
if codes_color_name == color_name:
color_id = color.find('COLOR').text
colorname.find('COLOR').text = str(color_id)
codes.write('3.xml')
break
if __name__ == '__main__':
pool = Pool(os.cpu_count())
pool.map(process_item, root_codes.findall('ITEM'))
我用 1.xml 的缩减版本尝试了这个,它基本上吐出相同的文件,没有任何改变。 有没有更有效的方法来做到这一点,或者我使用多处理的方式有什么问题?
使用多处理是另一回事。 让我们首先关注优化它。
代码在这里的工作方式实际上是做了很多不必要的处理,比如每次从 1.xml 中查找颜色的值,同时它可以预先存储和使用。 另外更新的时候在2.xml中每次更新都是找颜色,这里可以避免这两种查找,简单的遍历就可以解决问题。
因此,可以做些什么来实现这一点,就是维护一对字典(color_name,color_code)。 这个字典可以通过完全遍历 1.xml 一次来完成,然后在准备好这个字典之后,可以遍历 2.xml 的元素,并且可以从字典中获取每个元素的颜色代码值,并在更新所有元素的颜色代码之后2.xml的元素可以存储在3.xml中
import xml.etree.ElementTree as ET
codes = ET.parse('./datasets/1.xml')
root_codes = codes.getroot()
colors = ET.parse('./datasets/2.xml')
root_colors = colors.getroot()
done=0
color_code_dict = {}
for color_item in root_colors.iter('ITEM'):
color_code = color_item.find('COLOR').text
color_name = color_item.find('COLORNAME').text
color_code_dict[color_name] = color_code
for code_item in root_codes.iter('ITEM'):
code_color = code_item.find('COLOR').text
code_item.find('COLOR').text = color_code_dict.get(code_color)
codes.write('3.xml')
print("Done")
PS:确保从写入文件中获取颜色和代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.