Error converting pandas dataframe to xml saying Invalid Tag Name

huangapple go评论53阅读模式
英文:

Error converting pandas dataframe to xml saying Invalid Tag Name

问题

抱歉,我无法提供代码部分的翻译。以下是您提供的问题的翻译:

错误原因可能是您的DataFrame中包含了一个列名或标签,它包含空格或其他无效字符,例如"Loader TXN ID"。XML标签不允许包含空格或特殊字符。您需要确保将列名转换为有效的XML标签名称。您可以使用df.rename方法来更改列名,以便不包含无效字符,然后再尝试将DataFrame转换为XML。

英文:

I have dataframe like:

Loader   	TXN Description	   ANUMS	Value Date
67805499	ZZ-OPTR	           11504	1/27/2023 23:59
67805499	ZZ-OPTR	           33002	1/27/2023 23:59
67805499	ZZ-OPTR	           11504	1/27/2023 23:59
67805499	ZZ-OPTR	           33002	1/27/2023 23:59
67805501	ZZ-OPTR	           11504	1/27/2023 23:59
67805501	ZZ-OPTR	           33002	1/27/2023 23:59
67805501	ZZ-OPTR	           66666    1/27/2023 23:59

I want to convert this dataframe to xml and I used the following code:

import pandas as pd

df= pd.read_csv('C:\\Users\\ShDalal\\Desktop\\twosigma.csv')
with open('outputf.xml', 'w') as myfile: 
  myfile.write(df.to_xml())

But it throws error saying:

ValueError: Invalid tag name 'Loader TXN ID'

What can be the reason for it

答案1

得分: 2

尝试使用以下方式来写入文件:

df.to_xml('/path/to/file.xml')
英文:

why this file open. Try this one to write the file:

df.to_xml('/path/to/file.xml')

huangapple
  • 本文由 发表于 2023年2月7日 02:35:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365293.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定