一、错误描述
错误很简单,就是xml里面有非法字符(有的说是编码问题),所以才导致xml文件出现错误,pypthon的xml.etree.ElementTree
读取不了
我这里用记事本打开,发现是xml文件中有些地方出现了中文乱码
二、错误解决
我们可以再debug的时候看到每一行的text,我这里是已经修改过来,修改之前红框里面是中文乱码
我这里是把对应行的内容修改之后,再write到前面读取的xml_file = "F:\\e_1_1_4_7_0_4_1_5_49_60_0.xml"
了,代码如下:
import xml.etree.ElementTree as ET
import os
xml_file = "F:\\e_1_1_4_7_0_4_1_5_49_60_0.xml"
with open(xml_file, mode='r', errors='ignore') as f:
lines = f.readlines()
f.close()
lines[2] = ' <folder>F</folder>\n'
lines[4]=' <path>none</path>\n'
lines[25] = ' <name>none</name>\n'
with open(xml_file, mode='w', errors='ignore') as f:
for line in lines:
f.write(line)
f.close()
# input_dir='D:\\0.17SUO\\17所\e-两栖登陆舰\SAR\\'
input_dir='D:\\DATA\\'
filenames = os.listdir(input_dir)
for filename in filenames:
if filename.endswith('.xml'):
path = input_dir+filename
file_front = filename[:-4]
with open(path, mode='r', errors='ignore') as f:
lines = f.readlines()
f.close()
lines[2] = ' <folder>F</folder>\n'
lines[4]=' <path>'+file_front+'</path>\n'
lines[25] = ' <name>none</name>\n'
with open(path, mode='w', errors='ignore') as f:
for line in lines:
f.write(line)
f.close()