Ugrás a fő tartalomra

XML ből CSV előállítás pythonnal

Parszolás és az eredmény csv filebe mentése







Python kód:

import xml.etree.ElementTree as ET
import csv

def parse_xml(xml_file):
    plant_list = []                        
    tree = ET.parse(xml_file)               
    root = tree.getroot()                    

    for item in root.findall('./employee'):    
        plant = {}                          
        for child in item:
            plant[child.tag] = child.text.strip()    # add item to dict level1
            
            for child2 in child:
                plant[child2.tag] = child2.text.strip() # add item to dict level 2

        plant_list.append(plant)                            # add dict to the list

    return plant_list                  


def save_to_csv(data, csv_file):
    headers = ['name','phone','country','date','email']   # headers  list
    
    with open(csv_file,'w',  newline='', encoding='utf-8') as f:
        writer = csv.DictWriter(f, fieldnames = headers, delimiter ='|')      
        writer.writeheader()         # write headers to csv
        writer.writerows(data)       # write dictionary elements to csv


xml_file = r'c:\Users\User\Documents\mintak\xml\minta.xml'  
csv_file = r'minta3.csv' 

all_data = parse_xml(xml_file)
save_to_csv(all_data,csv_file)

all_data                           ## view data


Eredmény monitoron:

[{'name': 'Sita',
  'phone': '3290349906',
  'email': 'sita@example.com',
  'date': '2019-03-02 11:16:07',
  'country': 'India'},
 {'name': 'Sam',
  'phone': '9059098968',
  'email': 'sam@example.com',
  'date': '2019-06-01 10:06:07',
  'country': 'America'},
 {'name': 'Sammy',
  'phone': '6750390948',
  'email': 'sammy@example.com',
  'date': '2019-04-05 16:30:07',
  'country': 'Africa'},
 {'name': 'Akaya',
  'phone': '6750390948',
  'email': 'akaya@example.com',
  'date': '2019-04-05 16:30:07',
  'country': 'Afganistan'},
 {'name': 'Linda',
  'phone': '6750390948',
  'email': 'linda@example.com',
  'date': '2019-04-05 16:30:07',
  'country': 'Africa'}]



Eredmény csv fileben

name|phone|country|date|email
Sita|3290349906|India|2019-03-02 11:16:07|sita@example.com
Sam|9059098968|America|2019-06-01 10:06:07|sam@example.com
Sammy|6750390948|Africa|2019-04-05 16:30:07|sammy@example.com
Akaya|6750390948|Afganistan|2019-04-05 16:30:07|akaya@example.com
Linda|6750390948|Africa|2019-04-05 16:30:07|linda@example.com



 Forrás XML minta:

<?xml version="1.0"?>
<company>
<employee>
 <name>Sita</name>
 <phone>3290349906</phone>
 <email>sita@example.com</email>
 <date>2019-03-02 11:16:07</date>
       <country>India</country>
</employee>
<employee>
 <name>Sam</name>
 <phone>9059098968</phone>
 <email>sam@example.com</email>
 <date>2019-06-01 10:06:07</date>
  <country>America</country>
</employee>
<employee>
 <name>Sammy</name>
 <phone>6750390948</phone>
 <email>sammy@example.com</email>
 <date>2019-04-05 16:30:07</date>
  <country>Africa</country>
</employee>
   <employee>
 <name>Akaya</name>
 <phone>6750390948</phone>
 <email>akaya@example.com</email>
 <date>2019-04-05 16:30:07</date>
       <country>Afganistan</country>
</employee>
   <employee>
 <name>Linda</name>
 <phone>6750390948</phone>
 <email>linda@example.com</email>
 <date>2019-04-05 16:30:07</date>
       <country>Africa</country>
</employee>
</company>



XML fa truktura minta:









Megjegyzések