錯誤很簡單,就是xml裡面有非法字符(有的說是編碼問題),所以才導致xml文件出現錯誤,pypthon的xml.etree.ElementTree
讀取不了
I open it with notepad here,發現是xmlChinese garbled characters appear in some places in the file
我們可以再debugwhen you see each linetext,I have modified it here,Before the modification, there are Chinese garbled characters in the red box
I am here after modifying the content of the corresponding line,再writeread beforexml_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()