嗨,我來自 Stack Overflow 的朋友們,
我想使用 python將 csv 轉換為 xml,我聽說熊貓可以非常簡單地管理這項任務。
好吧,事實證明它并不那么容易。
我的代碼是什么樣的:
import pandas as pd
import chardet
from pandas.core.frame import DataFrame
csvFile = '172431-82056.csv'
xmlFile = 'mySecondData.xml'
def check_encoding(filename):
"""
input: filename = "filename.csv"
output: Dictionary = {'encoding': 'UTF-16', 'confidence': 1.0, 'language': ''}
"""
result= {}
with open(filename, 'rb') as rawdata:
result = chardet.detect(rawdata.read(10000))
return result
def import_csv(filename):
"""
input: filename = "filename.csv"
output: Dictionary = {'csv key': 'csv data', ... }
"""
encoding = check_encoding(filename)['encoding']
csv_data = pd.read_csv(filename, engine ='python', encoding=encoding, sep = None)
#print(csv_data)
return csv_data
#print(import_csv(csvFile))
def convert_to_xml(input_file, output_file):
csv_data = import_csv(input_file)
csv_data.to_xml(path_or_buffer=output_file, index = True, root_name='products',row_name='item', elem_cols=['post_title','regular_price'], prefix = 'g:', pretty_print=True)
convert_to_xml(csvFile, xmlFile)
我的輸出是什么樣的:
Traceback (most recent call last):
File "c:\Users\PavelH\Documents\Git\CSV Converter\csv_converter.py", line 53, in <module>
convert_to_xml(csvFile, xmlFile)
File "c:\Users\PavelH\Documents\Git\CSV Converter\csv_converter.py", line 51, in convert_to_xml
df.to_xml(path_or_buffer=output_file, index = True, root_name='products',row_name='item', prefix = 'g:', pretty_print=True)
File "C:\Users\PavelH\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\frame.py", line 2986, in to_xml
return xml_formatter.write_output()
File "C:\Users\PavelH\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\formats\xml.py", line 265, in write_output
xml_doc = self.build_tree()
File "C:\Users\PavelH\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\formats\xml.py", line 485, in build_tree
self.build_elems()
File "C:\Users\PavelH\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\formats\xml.py", line 575, in build_elems
SubElement(self.elem_row, elem_name).text = val
File "src\lxml\etree.pyx", line 3136, in lxml.etree.SubElement
File "src\lxml\apihelpers.pxi", line 179, in lxml.etree._makeSubElement
File "src\lxml\apihelpers.pxi", line 1734, in lxml.etree._tagValidOrRaise
ValueError: Invalid tag name 'foo bar'
帶空格的標簽是否無效?
uj5u.com熱心網友回復:
我認為你的熊貓已經過時了。to_xml已在 1.3.0 版中引入。你可以檢查你的版本
# in python shell
import pandas
print(pandas.__version__)
如果這是一個比 1.3.0 舊的版本,你應該用
# in bash shell
pip install --upgrade pandas
uj5u.com熱心網友回復:
命令pip install --upgrade pandas解決了這個問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/319267.html
