[code.view]

[top] / python / PyMOTW / xml / etree / ElementTree / ElementTree_csv_to_xml.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2010 Doug Hellmann.  All rights reserved.
     #
     """Convert CSV file of podcasts to XML
     """
     #end_pymotw_header
     
     import csv
     from xml.etree.ElementTree import Element, SubElement, Comment, tostring
     import datetime
     from ElementTree_pretty import prettify
     
     generated_on = str(datetime.datetime.now())
     
     # Configure one attribute with set()
     root = Element('opml')
     root.set('version', '1.0')
     
     root.append(Comment('Generated by ElementTree_csv_to_xml.py for PyMOTW'))
     
     head = SubElement(root, 'head')
     title = SubElement(head, 'title')
     title.text = 'My Podcasts'
     dc = SubElement(head, 'dateCreated')
     dc.text = generated_on
     dm = SubElement(head, 'dateModified')
     dm.text = generated_on
     
     body = SubElement(root, 'body')
     
     with open('podcasts.csv', 'rt') as f:
         current_group = None
         reader = csv.reader(f)
         for row in reader:
             group_name, podcast_name, xml_url, html_url = row
             if current_group is None or group_name != current_group.text:
                 # Start a new group
                 current_group = SubElement(body, 'outline', {'text':group_name})
             # Add this podcast to the group,
             # setting all of its attributes at
             # once.
             podcast = SubElement(current_group, 'outline',
                                  {'text':podcast_name,
                                   'xmlUrl':xml_url,
                                   'htmlUrl':html_url,
                                   })
     
     print prettify(root)
     

[top] / python / PyMOTW / xml / etree / ElementTree / ElementTree_csv_to_xml.py

contact | logmethods.com