python - XML 到数据帧
问题描述
我的代码没有在 Datafeame 中输入值 - 全部无,请帮助查找错误。最初我在这里取了代码 fron=m http://gokhanatil.com/2017/11/python-for-data-science-importing-xml-to-pandas-dataframe.html#comment-414932
我的 xml
xmlns="http://www.sec.gov/edgar/document /thirteenf/informationtable" xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance">
<infoTable>
<nameOfIssuer>AMERICAN AIRLS GROUP INC</nameOfIssuer>
<titleOfClass>COM</titleOfClass>
<cusip>02376R102</cusip>
<value>857267</value>
<shrsOrPrnAmt>
<sshPrnamt>20742000</sshPrnamt>
<sshPrnamtType>SH</sshPrnamtType>
</shrsOrPrnAmt>
<investmentDiscretion>DFND</investmentDiscretion>
<otherManager>4</otherManager>
<votingAuthority>
<Sole>20742000</Sole>
<Shared>0</Shared>
<None>0</None>
</votingAuthority>
我的代码
import xml.etree.cElementTree as et
import pandas as pd
def getvalueofnode(node):
""" return node text or None """
return node.text if node is not None else None
def main():
""" main """
parsed_xml = et.parse("form13fInfoTable.xml")
dfcols = ['infoTable/nameOfIssuer', 'infotanle/cusip', 'infotable/value']
df_xml = pd.DataFrame(columns=dfcols)
for node in parsed_xml.getroot():
print(parsed_xml.getroot())
parsed_xml.getroot()
nameOfIssuer = node.find('informationTable/infoTable/nameOfIssuer')
cusip = node.find('infoTable/cusip')
value = node.find('infoTable/value')
df_xml = df_xml.append(pd.Series([getvalueofnode(nameOfIssuer),getvalueofnode(cusip), getvalueofnode(value)], index=dfcols),ignore_index=True)
print(df_xml)
main()
解决方案
我会写评论,但我还不能这样做。
您的 xml 文件是否正确?似乎您的 xml 中缺少开始标记,其中一个是命名空间:
<root xmlns=... > your xml </root>
此外,<infoTable>
标签未关闭
推荐阅读
- mysql - mysqli 查询中的条件以将特定行的 where 条件从 column1 更改为 column2
- python-3.x - matplotlib - 模块“sip”没有属性“setapi”
- python - 将字符串列表转换为数值向量以计算汉明距离
- single-page-application - base64 编码网站图标有什么好处?
- ios - 为什么 VStack 在 iOS13 和 iOS14 之间有不同的行为?
- c# - 如何更改工具栏 Xamarin 表单中图标的大小
- python - 在 python 中使用验证循环计算运行总计
- swiftui - 均匀对齐swiftUI Stack中的项目
- ios - SwiftUI 列表选择没有价值
- azure - 如何将 Web Activity 的输出传递到 Copy Activity 进行存储