Error:

Parse XML string

import xml.etree.ElementTree as ET

xml = '<category name="cat5"> <article>http://quackquack42.com/art9238.xml</article> </category>'

root = ET.fromstring(xml)
print root.tag # category
print root.attrib # {'name': 'cat5'}

Parse XML files

import xml.etree.ElementTree as ET

tree = ET.parse('file.xml')
root = tree.getroot()
print root.tag # category
print root.attrib # {'name': 'cat5'}

Iterate over DOM structure

import xml.etree.ElementTree as ET

tree = ET.parse('file.xml')
root = tree.getroot()
for child in root:
    print child.text
    

Access XML data

import xml.etree.ElementTree as ET

tree = ET.parse('file.xml')
root = tree.getroot()
nodes = root.findall('article')
for node in nodes:
    print node.text

Serialize data to XML

import xml.etree.ElementTree as ET

articles = [
    'http://quackquack42.com/art9238.xml',
    'http://quackquack42.com/art9231.xml',
    'http://quackquack42.com/art9230.xml',

]
category = ET.Element('category', {'name': 'cat5'})
for link in articles:
    article = ET.SubElement(category, 'article')
    article.text = link
xml = ET.tostring(category)

Parse JSON string

import json
json_string = '{"author":{"first_name":"Alex","last_name":"T."},"title": "Some things..."}'
obj = json.loads(json_string)
print obj # {u'title': u'Some things...', u'author': {u'first_name': u'Alex', u'last_name': u'T.'}}

Parse JSON file

import json

infile = open('file.json')
obj = json.load(infile)
print obj # {u'title': u'Some things...', u'author': {u'first_name': u'Alex', u'last_name': u'T.'}}

Serialize data to JSON

import json
obj = {
  'title': 'Some things...', 
  'author': {
    'first_name': 'Alex', 
    'last_name': 'T.'
  }
}

json_string = json.dumps(obj)
# {"author": {"first_name": "Alex", "last_name": "T."}, "title": "Some things..."}

Parse CSV file

import csv
with open('test.csv', 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=',')
    for row in spamreader:
        # print first column
        print row[0]

Parse CSV string

import StringIO
import csv

scsv = """1,2,3
a,b,c"""

f = StringIO.StringIO(scsv)
reader = csv.reader(f, delimiter=',')
for row in reader:
    # print first column
    print row[0]

Generate CSV file

import csv
with open('out.csv', 'wb') as csvfile:
    writer = csv.writer(csvfile, delimiter=',')
    writer.writerow(range(1,10))
    writer.writerow(range(100,110))

Generate CSV string

import StringIO
import csv


f = StringIO.StringIO()
writer = csv.writer(f, delimiter=',')
writer.writerow(range(1,10))
writer.writerow(range(100,110))

print f.getvalue()

Parse YAML file

# install PyYAML: http://pyyaml.org/
import yaml

stream = open("test.yml", 'r')
obj = yaml.load(stream)