Skip to content

Instantly share code, notes, and snippets.

@yilas
Last active February 29, 2016 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yilas/8ce55f7c9a84c4e785f3 to your computer and use it in GitHub Desktop.
Save yilas/8ce55f7c9a84c4e785f3 to your computer and use it in GitHub Desktop.
# csv
import csv
file_csv = ''
csvfile = open(file_csv, 'rb')
reader = csv.reader(csvfile)
for row in reader:
print row
import csv
file_csv = ''
csvfile = open(file_csv, 'rb')
reader = csv.DictReader(csvfile)
for row in reader:
print row
# json
import json
json_data = open('data-text.json').read()
data = json.loads(json_data)
for item in data:
print item
# XML Data
from xml.etree import ElementTree as ET
tree = ET.parse('data-text.xml')
root = tree.getroot()
data = root.find('Data')
all_data = []
for observation in data:
record = {}
for item in observation:
lookup_key = item.attrib.keys()[0]
if lookup_key == 'Numeric':
rec_key = 'NUMERIC'
rec_value = item.attrib['Numeric']
else:
rec_key = item.attrib[lookup_key]
rec_value = item.attrib['Code']
record[rec_key] = rec_value
all_data.append(record)
print all_data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment