Skip to content

Instantly share code, notes, and snippets.

@rafaelnovello
Created August 17, 2011 20:38
Show Gist options
  • Save rafaelnovello/1152550 to your computer and use it in GitHub Desktop.
Save rafaelnovello/1152550 to your computer and use it in GitHub Desktop.
Just a simple case of conversion from xml to csv
from lxml import etree
import csv
file = open("users_utf.xml", "r")
mycsv = csv.writer(open("importcsv.csv", "wb"))
for line in file:
if "\"" in line:
line = line.replace("\"", "")
if "," in line:
line = line.replace(",", "")
root = etree.fromstring(line)
code = root.find("code").text
password = root.find("password").text
name = root.find("name").text
email = root.find("email").text
docList = root.find("docList")
if docList:
doc = docList.find("doc").text
try:
lastname = doc.encode("utf-8")
except:
lastname = ' '
else:
lastname = ' '
mycsv.writerow([name.encode("utf-8")[:35], lastname[:25], code.encode("utf-8"), password.encode("utf-8"), email.encode("utf-8")])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment