Я конвертирую 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