import xml.etree.ElementTree as et
import csv
xmltext = """
1375
Key 11375
Key 2Some String
Key 3Another string
Key 4Yet another string
Key 5Strings anyone?
"""
f = open('output.txt', 'w')
writer = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC)
tree = et.fromstring(xmltext)
# iterate over the dict elements
for dict_el in tree.iterfind('dict'):
data = []
# get the text contents of each non-key element
for el in dict_el:
if el.tag == 'string':
data.append(el.text)
# if it's an integer element convert to int so csv wont quote it
elif el.tag == 'integer':
data.append(int(el.text))
writer.writerow(data)