こんにちは私のXMLファイルをpandasデータフレームに変換できます。しかし、私が持っている挑戦は、私は適切な行にレコードを取得していない、我々は例えば繰り返しているXMLでタグのセットを持っていると言うことができます。 4回、それは私のデータフレームの列でなければならない複数の子ノードを持っています、今私はxmlを読もうとしています私は私のパンダのデータフレームでわずか4行を取得したいが、別のレベルにある。PythonのXML解析Pandasは1行に完全なタグブロックを取得します
編集:ちょうど今、XMLデータの記述/相違点を見つけました。言及されたものは最終的に編集されたXMLデータです 私のXMLデータでいくつかの問題を見つけてください...正確で最終的なXMLコンテンツを更新しました。私は以下の回答でパフェによって提案された今の機能として使用しています
<ns1:parenttag>
<ns1:indenticaltag>123</ns1:indenticaltag>
<ns1:childtag2>N</ns1:childtag2>
<ns1:childtag3>0</ns1:childtag3>
<ns1:childtag4>N</ns1:childtag4>
<ns1:childtag5>
<ns2:subchildtag1 attributename0="poi">
<ns2:sub_subchildtag1>
<ns2:sub_sub_subchildtag1>
<ns2:sub_sub_sub_subchildtag1 attributename1="3" attributename2="17">1234</ns2:sub_sub_sub_subchildtag1>
</ns2:sub_sub_subchildtag1>
</ns2:sub_subchildtag1>
</ns2:subchildtag1>
</ns1:childtag5>
<ns1:childtag6>12</ns1:childtag6>
<ns1:childtag7> qwer</ns1:childtag7>
<ns1:childtag8>
<ns3:subchildtag2>456</ns3:subchildtag2>
</ns1:childtag8>
<ns1:childtag9>N</ns1:childtag9>
<ns1:childtag10>
<ns3:subchildtag3>908</ns3:subchildtag3>
<ns3:subchildtag4>abc/123</ns3:subchildtag4>
</ns1:childtag10>
</ns1:parenttag>
: が、このエラーを取得:
i am getting ValueError: Length mismatch: Expected axis has 21 elements, new values have 22 elements erros
Also it has issue with indenticaltag column as its of same name thrice but hierarchy is different
but in dataframe i am getting only one indenticaltag column instead of 3 for example:
parent.child.indenticaltag, parent.child.subchild.indenticaltag and parent.child.subchild.sub_subchild.indenticaltag etc.
Same <ns1:parenttag> is getting repeated over a xml file multiple times
<?xml version="1.0" encoding="UTF-8"?>
<row:user-agents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:row="http://www.row.com"
xmlns:ns1="http://www.ns1.com"
xmlns:ns2="http://www.ns2.com"
xmlns:ns3="http://www.ns3.com"
xmlns:row1="http://www.row1.com"
xmlns:row3="http://www.row3.com"
xmlns:row2="http://www.row2.com"
xsi:schemaLocation="http://www.schemaLocation-1.4.xsd">
<row:agent1>
<row:test>
<row2:test1>
<row2:test2>
<row2:test3>9999</row2:test3>
<row2:test4>aa</row2:test4>
<row2:test5>1</row2:test5>
</row2:test2>
</row2:test1>
<row2:test6>2017</row2:test6>
</row:test>
<row:agent2>
<row3:agent3>
<ns1:parenttag>
<ns1:childtag1>
<ns1:subchildtag1>
<ns1:indenticaltag>123</ns1:indenticaltag>
</ns1:subchildtag1>
</ns1:childtag1>
<ns1:indenticaltag>456</ns1:indenticaltag>
<ns1:childtag2>N</ns1:childtag2>
<ns1:childtag3>0</ns1:childtag3>
<ns1:childtag4>N</ns1:childtag4>
<ns1:childtag5>
<ns2:subchildtag2 attributname="abc">
<ns2:sub_subchildtag1>12 45</ns2:sub_subchildtag1>
</ns2:subchildtag2>
</ns1:childtag5>
<ns1:childtag6>tyu</ns1:childtag6>
<ns1:childtag7>2</ns1:childtag7>
<ns1:childtag8> poiu</ns1:childtag8>
<ns1:childtag9>
<ns3:subchildtag3>345</ns3:subchildtag3>
<ns3:subchildtag6>567</ns3:subchildtag6>
</ns1:childtag9>
<ns1:childtag10>N</ns1:childtag10>
<ns1:childtag11>
<ns3:subchildtag4>34</ns3:subchildtag4>
<ns3:subchildtag5>abc/123</ns3:subchildtag5>
</ns1:childtag11>
<ns1:childtag12>
<ns1:indenticaltag>234</ns1:indenticaltag>
</ns1:childtag12>
</ns1:parenttag>
</row3:agent3>
</row:agent2>
</row:agent1>
</row:user-agents>
親タグの項で少し異なっている別のXMLに
出力データフレーム:
I will parse both xmls differently using one function only.
Would like to parse all the tags and their attribute as column name in
pandas. Also the column name should be
parent.child.subchild.sub_sub_subchildtag and for attributes it should
be parent.child.subchild.sub_sub_childtag.attribute
xmlを解析して適切な形式でレコードを取得するより良い方法はありますか?または私は何かが不足していますか?
編集:解決策はいくつかのより多くの複雑さを働いていたが、追加
I need your help for three points if you guys can suggest some pointers:
1) I need columns name for pandas dataframe as root.child.subchild.grandchild i am not sure how i can get it here ? as in my solution i was able to get.
2) the descendant function is very slow is any way we can speed it up ?
3) i have to multiple xml of same type present in one directory and i would like to generate one dataframe out of it by appending all xml results any best way to do ?
私よりもずっと優れています。私たちが壮大なグランド子供を階層内に持っているかどうかという疑問が1つあります。すべてのサブチャイルドをトラバースする標準的な方法はありますか? – user07
良い質問は、ノードインデックスで各 ' 'をループし、すべての子孫を解析するXPathの' descendant :: * 'を使って更新された拡張を見てください。 –
Parfait
あなたのXMLはどれくらいの大きさですか? 1 GB以上?あなたはどれくらい遅いですか?また、属性とテキストは非常に異なっています。あなたのXMLの例には属性や解析の試みが含まれていません。 **実際の**データの真の例を常に投稿してください。 – Parfait