Python Forum
parsing local xml files to csv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
parsing local xml files to csv
#4
(Feb-23-2019, 03:17 PM)erdem_ustunmu Wrote: how to get the tags and elements between the <dataDscr> ... </ dataDscr> tags,attributes and transfer them to csv.

How do I make a find and loop for <dataDscr><var> ... </ var></ dataDscr> tags?

There are too many xml files, So have to I ,to define all the tags and attributes that depend on them one by one?
You have to start testing as it's big file on first how to get data out,then think of structure wanted over to CSV.
from bs4 import BeautifulSoup

soup = BeautifulSoup(open('NPL_2008_LFS_v01_M_v01_A_ILOVAR.xml', encoding='utf-8'), 'xml')
data = soup.find('dataDscr')
So inside dataDscr there are many var tages.
Using find() get the first one,all is find_all().
look at data in first one.
>>> var = data.find('var')
>>> var
<var ID="V270" dcml="0" files="F6" intrvl="contin" name="PSU">
<location width="16"/>
<labl>
        PSU
      </labl>
<valrng>
<range max="1800" min="1001"/>
</valrng>
<sumStat type="vald">
        76208
      </sumStat>
<sumStat type="invd">
        0
      </sumStat>
<sumStat type="min">
        1001
      </sumStat>
<sumStat type="max">
        1800
      </sumStat>
<sumStat type="mean">
        1412.79
      </sumStat>
<sumStat type="stdev">
        231.955
      </sumStat>
<varFormat schema="other" type="numeric"/>
</var>

# All attributes
>>> var.attrs
{'ID': 'V270', 'dcml': '0', 'files': 'F6', 'intrvl': 'contin', 'name': 'PSU'}

# Get name
>>> var.attrs.get('name')
'PSU'

# All sumStat
>>> [i.text.strip() for i in var.find_all('sumStat')]
['76208', '0', '1001', '1800', '1412.79', '231.955']
>>> 
Reply


Messages In This Thread
parsing local xml files to csv - by erdem_ustunmu - Feb-23-2019, 11:30 AM
RE: parsing local xml files to csv - by snippsat - Feb-23-2019, 12:22 PM
RE: parsing local xml files to csv - by snippsat - Feb-23-2019, 10:41 PM
RE: parsing local xml files to csv - by snippsat - Feb-25-2019, 03:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  uploading files from a ubuntu local directory to Minio storage container dchilambo 0 512 Dec-22-2023, 07:17 AM
Last Post: dchilambo
  How to take the tar backup files form remote server to local server sivareddy 0 1,938 Jul-14-2021, 01:32 PM
Last Post: sivareddy
  opening files and output of parsing leodavinci1990 4 2,603 Oct-12-2020, 06:52 AM
Last Post: bowlofred
  Parsing Xml files >3gb using lxml iterparse Prit_Modi 2 2,395 May-16-2020, 06:53 AM
Last Post: Prit_Modi
  Parsing Attached .MSG Files with Python3 ericl42 1 3,710 Apr-12-2019, 06:28 PM
Last Post: ericl42
  Fetching html files from local directories shiva 3 3,463 Mar-20-2018, 05:12 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020