程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python error xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 3, column 50

編輯:Python

一、錯誤描述

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

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved