Skip to content

Instantly share code, notes, and snippets.

@AlexArcPy
Created March 19, 2016 20:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexArcPy/29d70d15a9db19e82976 to your computer and use it in GitHub Desktop.
Save AlexArcPy/29d70d15a9db19e82976 to your computer and use it in GitHub Desktop.
unicodecsv - read csv files
import unicodecsv
csv_file = r"C:\GIS\temp\graffiti.csv"
#read as is
with open(csv_file,"rb") as f:
reader = unicodecsv.DictReader(f)
reader_rows = list(reader)
print reader_rows[:3]
#[{u'OBJECTID': u'1', u'CID': u'11', u'POINT_X': u'-122.396707', u'SqFtAprox': u'5', u'FID': u'0', u'POINT_Y': u'37.7855429999'},
#{u'OBJECTID': u'2', u'CID': u'21', u'POINT_X': u'-122.411825', u'SqFtAprox': u'6', u'FID': u'1', u'POINT_Y': u'37.7739790002'},
#{u'OBJECTID': u'3', u'CID': u'28', u'POINT_X': u'-122.474009', u'SqFtAprox': u'4', u'FID': u'2', u'POINT_Y': u'37.7803529995'}]
#read only certain columns
fields_to_take = ['CID','POINT_X','POINT_Y']
with open(csv_file,"rb") as f:
reader = unicodecsv.DictReader(f)
reader_rows = list(reader)
reader_filtered_fields = [{field:dict_row[field] for field in fields_to_take}
for dict_row in reader_rows]
print reader_filtered_fields[:3]
#[{'POINT_X': u'-122.396707', 'POINT_Y': u'37.7855429999', 'CID': u'11'},
#{'POINT_X': u'-122.411825', 'POINT_Y': u'37.7739790002', 'CID': u'21'},
#{'POINT_X': u'-122.474009', 'POINT_Y': u'37.7803529995', 'CID': u'28'}]
#write to a new csv with a query
output_file_path = r"C:\GIS\temp\graffiti_filtered.csv"
fields_to_take = ['CID','POINT_X','POINT_Y']
with open(csv_file,"rb") as f:
reader = unicodecsv.reader(f)
headers = [field for field in reader.next() if field in fields_to_take]
with open(output_file_path,'ab') as output_file:
writer = unicodecsv.writer(output_file,delimiter=',')
writer.writerow(headers)
for row in reader:
if int(row[2]) > 2000:
row_filtered = [row[2],row[4],row[5]]
writer.writerow(row_filtered)
#three first rows of the output csv file
#CID,POINT_X,POINT_Y
#2022,-122.413350001,37.7692199998
#2093,-122.4635,37.7143009993
#2110,-122.410066,37.774991
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment