У меня есть набор данных в форме xml, и я хочу преобразовать это в табличную форму, чтобы я мог двигаться дальше в своем проекте. Приведен набор данных, и я также упомянул код, который я написал.
<?xml version='1.0' standalone='yes'?>
<docs>
<doc id="97636670" type="RADIOLOGY_REPORT">
<codes>
<code origin="CMC_MAJORITY" type="ICD-9-CM">786.2</code>
<code origin="COMPANY3" type="ICD-9-CM">786.2</code>
<code origin="COMPANY1" type="ICD-9-CM">204.0</code>
<code origin="COMPANY1" type="ICD-9-CM">786.2</code>
<code origin="COMPANY1" type="ICD-9-CM">V42.81</code>
<code origin="COMPANY2" type="ICD-9-CM">204.00</code>
<code origin="COMPANY2" type="ICD-9-CM">786.2</code>
</codes>
<texts>
<text origin="CCHMC_RADIOLOGY" type="CLINICAL_HISTORY">Eleven year old with ALL, bone marrow transplant on Jan. 2, now with three day history of cough.</text>
<text origin="CCHMC_RADIOLOGY" type="IMPRESSION">1. No focal pneumonia. Likely chronic changes at the left lung base. 2. Mild anterior wedging of the thoracic vertebral bodies.</text>
</texts>
</doc>
<doc id="97638013" type="RADIOLOGY_REPORT">
<codes>
<code origin="CMC_MAJORITY" type="ICD-9-CM">786.2</code>
<code origin="COMPANY3" type="ICD-9-CM">786.2</code>
<code origin="COMPANY1" type="ICD-9-CM">786.2</code>
<code origin="COMPANY2" type="ICD-9-CM">786.2</code>
</codes>
<texts>
<text origin="CCHMC_RADIOLOGY" type="CLINICAL_HISTORY">Six year old with history of cough for one week.</text>
<text origin="CCHMC_RADIOLOGY" type="IMPRESSION">The lungs are clear. Question prominent hilar lymph nodes, left greater than right, which could be reactive in nature.</text>
</texts>
</doc>
Я хочу преобразовать это в табличную форму с помощью библиотеки pandas и xml .etree.ElementTree
это мой код
import pandas as pd
import xml.etree.ElementTree as et
xtree = et.parse("./Downloads/2007ChallengeTestDataNoCodes.xml")
xroot = xtree.getroot()
df_cols = ["id", "type", "origin", "origin_type", "text"]
rows = []
for node in xroot.findall('doc'):
att = node.attrib
typ = att.get('type')
i = att.get('id')
for n in xroot.findall('text'):
attr = n.attrib
orig = attr.get('origin')
orig_typ = attr.get('type')
txt = att.get('text')
rows.append({"id": i, "type": typ, "origin": orig,
"origin_type": orig_typ, "text": txt})
out_df = pd.DataFrame(rows, columns = df_cols)
print(out_df)
но мой код не дает вывода, я не знаю, в чем проблема.
Empty DataFrame
Columns: [id, type, origin, origin_type, text]
Index: []