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
#8
hi snippsat;
I tried to get and combine the categories according to the code you wrote yesterday.

for cat in var.find_all('catgry'):
        
        cat_value=[value.text.strip() for value in cat.find_all('catValu')]
        cat_label=[value.text.strip() for value in cat.find_all('labl')]
        cat_stat=[value.text.strip() for value in cat.find_all('catStat')]
        categories=dict(list(zip(cat_value, cat_label,cat_stat)))
        print(categories)


When I tried to do it yesterday, it brought the latest category, not all categories.
Now I've tried to make it look like you wrote it, but it didn't.

I wonder if I'm making a logical mistake.

so far codes:
from bs4 import BeautifulSoup
lst = []
categories = []
soup = BeautifulSoup(open('NPL_2008_LFS_v01_M_v01_A_ILOVAR.xml', encoding='utf-8'), 'xml')
title = soup.find('titl')
producer = soup.find('producer')
data = soup.find('dataDscr')
vars = data.find_all('var')
for var in vars:
    
    ID=var.attrs.get('ID')
    name=var.attrs.get('name')
    files=var.attrs.get('files')
    dcml=var.attrs.get('dcml')
    intrvl=var.attrs.get('intrvl')
    labl=var.find('labl').text.strip()
   
    sum_Stat=[i.text.strip() for i in var.find_all('sumStat')] 
    sum_Att = [i.attrs['type'] for i in var.find_all('sumStat')] 
   
    sumStat=dict(list(zip(sum_Att, sum_Stat))) 
    VarFormat=(var.find('varFormat')).attrs.get('type')
    stdCatgry = [stdCat.text.strip() for stdCat in  var.find_all("stdCatgry")]
    Range = [i.attrs for i in var.find_all('range')]
    

    
    for cat in var.find_all('catgry'):
        
        cat_value=[value.text.strip() for value in cat.find_all('catValu')]
        cat_label=[value.text.strip() for value in cat.find_all('labl')]
        cat_stat=[value.text.strip() for value in cat.find_all('catStat')]
        categories=dict(list(zip(cat_value, cat_label,cat_stat)))
        print(categories)
        

        
    print(title.text.strip(),producer.text.strip(),ID,name,files,dcml,intrvl,labl,sumStat,VarFormat,stdCatgry,Range,categories)
    lst.append((title.text.strip(),producer.text.strip(),ID,name,files,dcml,intrvl,labl,sumStat,VarFormat,stdCatgry,Range,categories))
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
RE: parsing local xml files to csv - by erdem_ustunmu - Feb-26-2019, 01:58 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,602 Oct-12-2020, 06:52 AM
Last Post: bowlofred
  Parsing Xml files >3gb using lxml iterparse Prit_Modi 2 2,394 May-16-2020, 06:53 AM
Last Post: Prit_Modi
  Parsing Attached .MSG Files with Python3 ericl42 1 3,707 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