Krish1992
Krish1992

Reputation: 81

Read xml file pandas from given tag

My xml data is as follows,

enter image description here

I want output dataframe as

English Marathi
prompt_username वापरकर्त्याचे नाव
item आगमन चेतावणी

The output is kind of english to marathi table.

I tried with following code:

xml_file = et.parse("strings_Marathi.xml")
for item in xml_file.iterfind('string name='):
    print(item)

I could find string tage but how can I extract string name="" ?

Can someone help me with this.

Thanks.

Upvotes: 0

Views: 427

Answers (1)

user5386938
user5386938

Reputation:

name is an attribute of the element string. To get the attribute value, you use the property .attrib which is a dictionary.

from xml.etree import ElementTree as ET
from io import StringIO

data = '''\
<foo>
<string name="abc">123</string>
<string name="def">456</string>
<string>789</string>
<string name="jkl">000</string>
</foo>
'''

f = StringIO(data)
tree = ET.parse(f) # replace with ET.parse("strings_Marathi.xml") to load your file

for tag in tree.iterfind('.//string[@name]'):
    print(tag.attrib['name'], tag.text)

Upvotes: 1

Related Questions