Created
August 17, 2011 20:38
-
-
Save rafaelnovello/1152550 to your computer and use it in GitHub Desktop.
Just a simple case of conversion from xml to csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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