【问题标题】:xml parsing to do iterationxml解析做迭代
【发布时间】:2020-06-22 18:39:03
【问题描述】:

我有一个 xml 文件

<PhysicalTable mdsid="mc000002a-469e-1667-ba67-0a890c480000">
        <name>FCT_PBCS</name>
        <xmlnsxsi>http://www.w3.org/2001/XMLSchema-instance</xmlnsxsi>
        <xmlns>http://www.oracle.com/obis/repository</xmlns>
        <type>none</type> 
        <maxConn>0</maxConn>
        <containerRef>"/oracle/bi/server/base/Schema/80000002-469e-1667-ba67-0a890c480000.xml#m80000002-469e-1667-ba67-0a890c480000"</containerRef>
   </PhysicalTable>
   <PhysicalColumn mdsid = "mc000002b-469e-1667-ba67-0a890c480000">
        <name>ACCT_ID</name>
        <dataType>DOUBLE </dataType>
        <precision>22</precision> 
        <nullable>true</nullable> 
        <specialType>none</specialType>
    </PhysicalColumn>
    <PhysicalColumn mdsid = "mc000002c-469e-1667-ba67-0a890c480000">    
        <name>CURR_ID</name>
        <dataType>DOUBLE</dataType> 
        <precision>22</precision>
        <nullable>true</nullable>
        <specialType>none</specialType>
    </PhysicalColumn>
    <PhysicalColumn mdsid = "mc000002d-469e-1667-ba67-0a890c480000">    
        <name>DATE_KEY</name>
        <dataType>DOUBLE</dataType> 
        <precision>22</precision>
        <nullable>true</nullable>
        <specialType>none</specialType>
    </PhysicalColumn>

我正在尝试解析并将所有值放入数据框中

for node in xroot.findall("PhysicalColumn"):
    s_mdsid = node.attrib.get("mdsid")
    s_name = node.find("name").text if node is not None else None
    s_dataType = node.find("dataType").text if node is not None else None
    s_precision = node.find("precision").text if node is not None else None
    s_nullable = node.find("nullable").text if node is not None else None
    s_specialType = node.find("specialType").text if node is not None else None
        
rows.append({"mdsid": s_mdsid, "name": s_name, 
              "dataType": s_dataType, "precision": s_precision , "nullable" : s_nullable ,"specialType" : s_specialType})

out_df1 = pd.DataFrame(rows, columns = df_cols1)

但是 out_df1 只给出了最后一个孩子的价值,它没有迭代。但是当我打印它时,它给了我整个儿童清单。我必须放入一个数据框并复制到excel。有人可以帮忙吗?

我得到的输出

    mdsid                                   name    dataType    precision   nullable    specialType
0   mc0000037-469e-1667-ba67-0a890c480000   YEAR_YY DOUBLE      22          true        none

我期待

    mdsid                                   name    dataType    precision   nullable    specialType
0   mc000002b-469e-1667-ba67-0a890c480000   ACCT_ID DOUBLE      22          true        none
1   mc000002b-469e-1667-ba67-0a890c480000   CURR_ID DOUBLE      22          true        none
....
13  mc0000037-469e-1667-ba67-0a890c480000   YEAR_YY DOUBLE      22          true        none

谢谢, 奥鲁什

【问题讨论】:

  • 检查 rows.append 行的缩进,也许?

标签: xml parsing


【解决方案1】:

你应该尝试这样的方法,看看它是否有效:

rows = []
for node in xroot.findall("PhysicalColumn"):
    s_mdsid = node.attrib.get("mdsid")  
    s_name = node.find("name").text if node is not None else None    
    s_dataType = node.find("dataType").text if node is not None else None
    s_precision = node.find("precision").text if node is not None else None
    s_nullable = node.find("nullable").text if node is not None else None
    s_specialType = node.find("specialType").text if node is not None else None
    row = []
    row.extend([s_mdsid, s_name,s_dataType, s_precision,s_nullable,s_specialType])
    rows.append(row)
columns = ["mdsid","name","dataType","precision","nullable","specialType"]
out_df1 = pd.DataFrame(rows,columns=columns)
out_df1

输出应该与您的预期输出相似。

【讨论】:

  • @Aarush 如果我们完成了,请不要忘记接受答案。
  • 你能告诉我在哪里可以找到接受答案的选项吗?我在任何地方都没有看到这样的选项..
  • @Aarush 您的帐户有些奇怪 - 您似乎没有选择接受的选项(通常是在拍手上方的复选标记);只支持或反对....
  • @Aarush 尝试点击拍手,看看是否有任何改变。
  • 我接受了答案,我已经发布了另一个问题,您能否澄清我的理解是否正确??
【解决方案2】:

我还有一个问题。我的实际 XML 文件是这样的

<?xml version="1.0" encoding="ISO-8859-1" ?>
<DECLARE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.oracle.com/obis/repository/admin">
<Database mdsid="m80000001-469e-1667-ba67-0a890c480000" name="PBCS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.oracle.com/obis/repository" type="Oracle11g" dbName="Oracle 11g" dbTypeId="57">
<Feature name="LEFT_OUTER_JOIN_SUPPORTED" value="true"/>
<Feature name="RIGHT_OUTER_JOIN_SUPPORTED" value="true"/>
<Feature name="FULL_OUTER_JOIN_SUPPORTED" value="true"/>
<Feature name="NESTED_OUTER_JOIN_SUPPORTED" value="true"/>
<Feature name="UNION_SUPPORTED" value="true"/>
<Feature name="UNION_ALL_SUPPORTED" value="true"/>
<Feature name="COUNT_SUPPORTED" value="true"/>
<Feature name="COUNT_DISTINCT_SUPPORTED" value="true"/>
<Feature name="COUNT_STAR_SUPPORTED" value="true"/>
<Feature name="SUM_SUPPORTED" value="true"/>
</Database>
<PhysicalTable mdsid="mc000002a-469e-1667-ba67-0a890c480000" name="FCT_PBCS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.oracle.com/obis/repository" type="none" maxConn="0" containerRef="/oracle/bi/server/base/Schema/80000002-469e-1667-ba67-0a890c480000.xml#m80000002-469e-1667-ba67-0a890c480000">
<PhysicalColumn mdsid="mc000002b-469e-1667-ba67-0a890c480000" name="ACCT_ID" dataType="DOUBLE" precision="22" nullable="true" specialType="none">
</PhysicalColumn>
<PhysicalColumn mdsid="mc000002c-469e-1667-ba67-0a890c480000" name="CURR_ID" dataType="DOUBLE" precision="22" nullable="true" specialType="none">
</PhysicalColumn>
<PhysicalColumn mdsid="mc000002d-469e-1667-ba67-0a890c480000" name="DATE_KEY" dataType="DOUBLE" precision="22" nullable="true" specialType="none">
</PhysicalColumn>
<PhysicalColumn mdsid="mc000002e-469e-1667-ba67-0a890c480000" name="ENTITY_ID" dataType="DOUBLE" precision="22" nullable="true" specialType="none">
</PhysicalColumn>
</PhysicalTable> 

元素树会在没有正确对齐根或属性的情况下解析这样的解析吗??

我认为它无法解析并建议更改输入文件,就像我之前发布的问题一样。

我的理解正确吗???谁能澄清一下

谢谢 奥鲁什

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    相关资源
    最近更新 更多