錯誤很簡單,就是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()