Python:用多处理替换xml元素

Python:用多处理替换xml元素,python,xml,multiprocessing,python-multiprocessing,xml.etree,Python,Xml,Multiprocessing,Python Multiprocessing,Xml.etree,我有两个不同的.xml文件,我想创建一个程序,用2.xml中的子文件替换1.xml中的子文件 1.xml如下所示: <CATALOG> <ITEM> <COLOR>11</COLOR> <COLORNAME>Black</COLORNAME> </ITEM> <ITEM> <COLOR>41</COLOR> &l

我有两个不同的.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>
我用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中每次更新时都会查找要更新的颜色,在这里可以避免这两种搜索,只需简单的遍历即可解决问题

所以,要做到这一点,可以做的是维护一个dict对(color\u name,color\u code)。只需完全遍历1.xml一次即可完成此dict,然后在准备好此dict后,可遍历2.xml的元素,并可从dict中获取每个元素的颜色代码值,更新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")
注意:确保从写入文件中提取颜色和代码

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)
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'))
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")