仅获取getvalueofnode数据帧中的第一行(节点.查找())

2024-06-07 06:43:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我进行了一个API调用,并希望通过响应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()

我得到的结果数据帧如下:Response code


Tags: comnodehttpmodelxmlassetfindxsd
1条回答
网友
1楼 · 发布于 2024-06-07 06:43:38

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

希望能回答你的问题,如果你需要我澄清什么请让我知道。:)

相关问题 更多 >