python - 仅使用 getvalueofnode(node.find()) 在 pandas Dataframe 中获取第一行
问题描述
我已经进行了 API 调用,并希望遍历响应 xml 以将相关值提取到数据框。该代码之前运行良好,但现在它显然不想返回超过每个节点/列的第一个值。
这是我的响应 XML:
<?xml version="1.0" encoding="utf-8"?>
<Assets xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<IsLastPage>true</IsLastPage>
<AssetRecords>
<Asset url="https://api.myvisionlink.com/APIService/VLReady/assets/single/1486128866430645">
<VisionLinkIdentifier>1486128866430645</VisionLinkIdentifier>
<MakeCode>CAT</MakeCode>
<MakeName>CAT</MakeName>
<SerialNumber>PNL00585</SerialNumber>
<AssetID>10-143</AssetID>
<EquipmentVIN/>
<Model>320ELRR</Model>
<ProductFamily>TRACK EXCAVATORS</ProductFamily>
<ManufactureYear>2015</ManufactureYear>
</Asset>
<Asset url="https://api.myvisionlink.com/APIService/VLReady/assets/single/2278960667345107">
<VisionLinkIdentifier>2278960667345107</VisionLinkIdentifier>
<MakeCode>CAT</MakeCode>
<MakeName>CAT</MakeName>
<SerialNumber>HBT20130</SerialNumber>
<AssetID>10-160</AssetID>
<EquipmentVIN/>
<Model>330FL</Model>
等等
这是我的代码:
r = session.get("https://api.myvisionlink.com/APIService/VLReady/Assets/1", headers={'Content-Type':'application/xml'})
def getvalueofnode(node):
return node.text if node is not None else None
def main():
root = cET.fromstring(r.content)
ns = {"xsd":"http://fms-standard.com/rfms/v1.0.0/xsd/position",
"xsi":"http://fms-standard.com/rfms/v1.0.0/xsd/common/position"}
data_list = [{'Make': getvalueofnode(node.find('Asset/MakeName', ns)),
'SerialNumber': getvalueofnode(node.find('Asset/SerialNumber', ns)),
'AssetID': getvalueofnode(node.find('Asset/AssetID', ns)),
'Model': getvalueofnode(node.find('Asset/Model', ns)),
'ProductFamily': getvalueofnode(node.find('Asset/ProductFamily', ns)),
'ManufactureYear': getvalueofnode(node.find('Asset/ManufactureYear', ns))} for node in root]
global df_xml
df_xml = pd.DataFrame(data_list)
main()
解决方案
我不确定您从 API 调用中获得的结果,但 xml 在您随问题提供的示例中看起来格式不正确。如果 XML 的结构不同,如果资产元素位于 XML 结构的根目录中,那么您的代码就会起作用。
您只获得第一条记录的原因是因为您正在遍历“IsLastPage”元素和“AssetRecords”元素,并且由于您使用的是 find() 而不是 findall(),所以一旦找到它就会停止第一场比赛。如果您想继续使用 find() 而不是 findall(),则必须修改代码以遍历“AssetRecords”元素,这是我在下面的代码中修改的内容。
def main():
root = et.fromstring(xml)
ns = {"xsd":"http://fms-standard.com/rfms/v1.0.0/xsd/position",
"xsi":"http://fms-standard.com/rfms/v1.0.0/xsd/common/position"}
# Find AssetRecords element
asset_records = root.find("AssetRecords")
data_list = [{'Make': getvalueofnode(node.find('MakeName', ns)),
'SerialNumber': getvalueofnode(node.find('SerialNumber', ns)),
'AssetID': getvalueofnode(node.find('AssetID', ns)),
'Model': getvalueofnode(node.find('Model', ns)),
'ProductFamily': getvalueofnode(node.find('ProductFamily', ns)),
'ManufactureYear': getvalueofnode(node.find('ManufactureYear', ns))} for node in asset_records]
global df_xml
df_xml = pd.DataFrame(data_list)
Output:
Make SerialNumber AssetID Model ProductFamily ManufactureYear
0 CAT PNL00585 10-143 320ELRR TRACK EXCAVATORS 2015
1 CAT HBT20130 10-160 330FL TRACK EXCAVATORS 2015
希望能回答您的问题,如果您需要我澄清任何事情,请告诉我。:)
推荐阅读
- google-apps-script - 如何根据另一列中的变量从一列中获取数据
- arrays - 使用 VBA 将过滤后的行复制到另一个工作表中
- c# - 为什么 C# 编译器允许我在操作符方法中访问对象的私有字段?
- ruby-on-rails - 在 Rails 中循环遍历 form.select(使用 Elastic Search/Searchkick aggs)
- java - 在新服务器上启动 grails 应用程序时出错(创建名称为“sessionFactory”的 bean 时出错)
- node.js - 无法托管开发服务器以在 vs 代码中做出反应
- r - 时间序列、lubridate 和 seq 函数
- python - Scrapy 在抓取 .robots.txt 时会产生垃圾
- c - 警告赋值从没有强制转换的指针生成整数。浮动 findMeanLine(int n ,int my2DArray[][n]) , findMeanCol(int n ,int my2DArray[][n])
- java - 带字母的菱形 - Java