-
Notifications
You must be signed in to change notification settings - Fork 16
/
csv_things.py
41 lines (37 loc) · 1.41 KB
/
csv_things.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import unicodecsv
def import_csv_to_list(filename, headers = False, astuple = False):
data = []
with open(filename, 'rb') as file:
reader = unicodecsv.reader(file, delimiter = ',', quotechar = '"')
if headers == True:
next(reader, None) # Skip header row.
for row in reader:
if(astuple):
data.append(tuple(row))
else:
data.append(row)
return data
def import_csv_to_dict(filename, params = None, headers = False):
data = []
with open(filename, 'rU') as file:
reader = unicodecsv.reader(file, delimiter = ',', quotechar = '"')
if(headers):
params = reader.next()
for row in reader:
data.append(dict(zip(params, row)))
return data
def export_list_to_csv(filename, data, headers = None):
with open(filename, 'wb') as file:
writer = unicodecsv.writer(file, delimiter = ',', quotechar = '"')
if headers != None:
writer.writerow(headers)
for i in range(len(data)):
writer.writerow(data[i])
return
def export_dict_to_csv(filename, data):
with open(filename, 'wb') as file:
writer = unicodecsv.writer(file, delimiter = ',', quotechar = '"')
writer.writerow(data[0].keys())
for i in range(len(data)):
writer.writerow(data[i].values())
return