使用 minidom.toprettyxml 时出错
Errors while using minidom.toprettyxml
我正在将 CSV 转换为 XML。输入文件 myData.csv:
id,code name,value
36,abc,7.6
40,def,3.6
9,ghi,6.3
76,def,99
Python 代码有效,但结果是一个字符串,没有换行符和缩进,所以我用 prettify 修改了它,但它产生了错误。感谢您对如何解决此问题的见解。
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import codecs
import csv
import sys
import os
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import *
from xml.dom import minidom
def prettify(elem):
"""Return a pretty-printed XML string for the Element.
"""
rough_string = ET.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
argvs = sys.argv
fileName = argvs[1]
with open(fileName, 'r', encoding='UTF-8') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
header = next(reader)
root = ET.Element('items')
dataNum = len(header)
for row in reader:
item = ET.SubElement(root,'item')
if len(row) == dataNum:
for i in range(dataNum):
node = ET.SubElement(item,header[i])
node.text = str(row[i])
#xmlFile = ET.ElementTree(root) # works but in one line, not pretty
xmlFile = prettify(root) # does not work, error
xmlFile.write("./"+fileName.replace("csv","xml"))
错误:
python3 csvToXml-test.py myData.csv
Traceback (most recent call last):
File "csvToXml-test.py", line 46, in <module>
xmlFile = prettify(root) # does not work, error
File "csvToXml-test.py", line 17, in prettify
reparsed = minidom.parseString(rough_string)
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/xml/dom/minidom.py", line 1968, in parseString
return expatbuilder.parseString(string)
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/xml/dom/expatbuilder.py", line 925, in parseString
return builder.parseString(string)
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/xml/dom/expatbuilder.py", line 223, in parseString
parser.Parse(string, True)
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 1, column 34
想通了。我在输入文件的 header 名称中有一个 space。花了我 2 个小时!
我正在将 CSV 转换为 XML。输入文件 myData.csv:
id,code name,value
36,abc,7.6
40,def,3.6
9,ghi,6.3
76,def,99
Python 代码有效,但结果是一个字符串,没有换行符和缩进,所以我用 prettify 修改了它,但它产生了错误。感谢您对如何解决此问题的见解。
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import codecs
import csv
import sys
import os
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import *
from xml.dom import minidom
def prettify(elem):
"""Return a pretty-printed XML string for the Element.
"""
rough_string = ET.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
argvs = sys.argv
fileName = argvs[1]
with open(fileName, 'r', encoding='UTF-8') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
header = next(reader)
root = ET.Element('items')
dataNum = len(header)
for row in reader:
item = ET.SubElement(root,'item')
if len(row) == dataNum:
for i in range(dataNum):
node = ET.SubElement(item,header[i])
node.text = str(row[i])
#xmlFile = ET.ElementTree(root) # works but in one line, not pretty
xmlFile = prettify(root) # does not work, error
xmlFile.write("./"+fileName.replace("csv","xml"))
错误:
python3 csvToXml-test.py myData.csv
Traceback (most recent call last):
File "csvToXml-test.py", line 46, in <module>
xmlFile = prettify(root) # does not work, error
File "csvToXml-test.py", line 17, in prettify
reparsed = minidom.parseString(rough_string)
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/xml/dom/minidom.py", line 1968, in parseString
return expatbuilder.parseString(string)
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/xml/dom/expatbuilder.py", line 925, in parseString
return builder.parseString(string)
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/xml/dom/expatbuilder.py", line 223, in parseString
parser.Parse(string, True)
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 1, column 34
想通了。我在输入文件的 header 名称中有一个 space。花了我 2 个小时!