I want to print data from an XML file. To do that, I created a dictionary to parse the file. Finally, I used a for loop to print the data in a new DataFrame.
<BREVIER>
<BRV>
<MONO>stuff</MONO>
<TITD>stuff</TITD>
<TITF>Blabla</TITF>
<CMPD>stuff</CMPD>
<CMPF>stuff</CMPF>
<INDD>stuff</INDD>
<INDF>Blablo</INDF>
<CINDD>stuff</CINDD>
<CINDF>stuff</CINDF>
<POSD>stuff</POSD>
<POSF>stuff</POSF>
<DEL>true</DEL>
</BRV>
and so on with many, many BRV categories.
The output I expect to have:
Nom_du_medicament Indication
Blabla Blablo
I tried this code:
# encoding: utf-8
import xmltodict
import pprint
import json
import pandas as pd
with open('Brevier.xml',encoding='UTF-8','rb') as fd:
my_dict = xmltodict.parse(fd.read(),encoding='UTF-8')
tableau_indic=pd.DataFrame()
for section in my_dict ['BREVIER']['BRV']:
drugname = section.get('TITF')
print(drugname in tableau_indic.loc(["Nom_du_medicament"]))
drugindication = section.get('INDF')
print(drugindication in tableau_indic.loc(["Indication"]))
print(tableau_indic)
fd.close()
I am getting a type error TypeError: unhashable type: 'list'
Since it did not work, here is the second method I tried using .loc:
# encoding: utf-8
import xmltodict
import pprint
import json
import pandas as pd
with open('Brevier.xml',encoding='UTF-8') as fd:
my_dict = xmltodict.parse(fd.read(),encoding='UTF-8')
tableau_indic=pd.DataFrame
for section in my_dict ['BREVIER']['BRV']:
drugname = section.get('TITF')
print(tableau_indic.loc["Nom_du_medicament"])
drugindication = section.get('INDF')
print(tableau_indic.loc["Indication"])
print(tableau_indic)
fd.close()
This time I had the KeyError: 'Nom_du_medicament' error.
Is there a way to avoid these errors?