使用熊猫解析xml

时间:2018-07-24 05:18:27

标签: python xml pandas

尝试解析xml,然后将其重新呈现为Pandas数据框

<?xml version="1.0"?><results>
<header>
  <cloc_url>github.com/AlDanial/cloc</cloc_url>
  <cloc_version>1.74</cloc_version>
  <elapsed_seconds>0.940369129180908</elapsed_seconds>
  <n_files>124</n_files>
  <n_lines>8440</n_lines>
  <files_per_second>131.863112209998</files_per_second>
  <lines_per_second>8975.19892784178</lines_per_second>
  <report_file>/Users/hariomsingh/Desktop/ignitechute/Repo/ignite-chute-aem_cloc.xml</report_file>
</header>
<files>
  <file name="/Users/hariomsingh/Desktop/ignitechute/Repo/ignite-chute-aem/aem-parent/pom.xml" blank="13" comment="23" code="491"  language="Maven" />
  <file name="/Users/hariomsingh/Desktop/ignitechute/Repo/ignite-chute-aem/aem-core/aem-core-bundle/src/test/resources/assets.json" blank="0" comment="0" code="357"  language="JSON" />
  <file name="/Users/hariomsingh/Desktop/ignitechute/Repo/ignite-chute-aem/aem-core/aem-core-bundle/src/main/java/com/chute/aem/core/api/impl/UserServiceImpl.java" blank="26" comment="21" code="202"  language="Java" />

输出类似

file name                                 blank  comment language code
Repo/ignite-chute-aem/aem-parent/pom.xml"  "13"   "23"     Maven   491
<fullpath>/assets.json"                     "12"   "3"      c       432

我只能做几行

import pandas as pd
from xml.etree import ElementTree
tree = ElementTree.parse('/Users/hariomsingh/Desktop/individualxml/ignite-chute-aem_cloc.xml')
root = tree.getroot()

print(root)
print(tree.iter())

csv_data = []
fields =  ['file name','blank','comment', 'language', 'code']

1 个答案:

答案 0 :(得分:1)

假设您可以安装beautifulsoup4(即Rails.application.routes.disable_clear_and_finalize = true #add this line Rails.application.routes.draw do #new route end )和熊猫(即pip3 install beautifulsoup4),那么就可以做到这一点:

pip3 install pandas