首页 > 解决方案 > 仅使用 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()

我得到的结果DataFrame如下:响应代码

标签: pythonpandasdataframe

解决方案


我不确定您从 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

希望能回答您的问题,如果您需要我澄清任何事情,请告诉我。:)


推荐阅读