xml в табличную форму в python - PullRequest
0 голосов
/ 01 мая 2020

У меня есть набор данных в форме 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: []

1 Ответ

1 голос
/ 02 мая 2020

Я исправил твой код. Вам нужно было добавить al oop для дополнительного уровня do c -> тексты -> текст. А также исправлено несколько мелких ошибок. Наслаждайтесь !!

xtree = et.parse("a.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 node.findall('texts'):
        for n1 in n.findall('text'):
            attr = n1.attrib
            orig = attr.get('origin')
            orig_typ = attr.get('type')
            txt = n1.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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...