try bs4...?
from bs4 import BeautifulSoup
page = '''
<camera>
<maker>Fujifilm</maker>
<model>GFX 50S</model>
<mount>Fujifilm G</mount>
<cropfactor>0.79</cropfactor>
</camera>
'''
soup = BeautifulSoup(page, 'lxml')
make = soup.find('maker')
model = soup.find('model')
print(f'Make: {make.text}\nModel: {model.text}')
for multiple entries, just loop through them with find_all()
from bs4 import BeautifulSoup
page = '''
<camera>
<maker>Fujifilm</maker>
<model>GFX 50S</model>
<mount>Fujifilm G</mount>
<cropfactor>0.79</cropfactor>
</camera>
<camera>
<maker>thing1</maker>
<model>thing2</model>
<mount>Fujifilm G</mount>
<cropfactor>0.79</cropfactor>
</camera>
<camera>
<maker>thing3</maker>
<model>thing4</model>
<mount>Fujifilm G</mount>
<cropfactor>0.79</cropfactor>
</camera>
<camera>
<maker>thing5</maker>
<model>thing6</model>
<mount>Fujifilm G</mount>
<cropfactor>0.79</cropfactor>
</camera>
'''
soup = BeautifulSoup(page, 'lxml')
make = soup.find_all('maker')
model = soup.find_all('model')
for x, y in zip(make, model):
print(f'Make: {x.text}\nModel: {y.text}')
getting data through a file:
from bs4 import BeautifulSoup
with open('path/to/your/file') as file:
page = file.read()
soup = BeautifulSoup(page, 'lxml')
make = soup.find_all('maker')
model = soup.find_all('model')
for x, y in zip(make, model):
print(f'Make: {x.text}\nModel: {y.text}')
without importing any modules:
with open('/PATH/TO/YOUR/FILE') as file:
for line in file:
for each in line.split():
if "maker" in each:
each = each.replace("<maker>", "")
print(each.replace("</maker>", ""))
this is for the 'maker' tag only, it might be beneficial to split these up into separate definitions and iterate through them
import xmltodict with open('c:\\temp\data.xml') as fd: doc = xmltodict.parse(fd.read()) print(doc['camera']['maker']) print(doc['camera']['model']) https://docs.python-guide.org/scenarios/xml/
– Any Moose Jul 25 '18 at 18:48