-
Notifications
You must be signed in to change notification settings - Fork 1
/
20-csv-and-python.py
33 lines (26 loc) · 1.06 KB
/
20-csv-and-python.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
import csv
import datetime
def generate_csv(a_list):
dict_list = []
for itemSet in a_list:
temp_dict = dict((x, y) for x, y in itemSet)
temp_dict['date'] = temp_dict['date'].strftime('%m/%d/%Y')
temp_dict['locations'] = ','.join(temp_dict['locations'])
dict_list.append(temp_dict)
print(dict_list)
with open('results.csv', 'w', newline='') as csvfile:
fieldnames = ['temperature', 'date', 'locations', 'weather']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for item in dict_list:
writer.writerow(item)
def parse_csv():
dict_list = []
with open('students.csv', 'r', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
month, day, year = row['Birthdate'].split('/')
row['Birthdate'] = datetime.date(int(year), int(month), int(day))
row['Marks'] = [int(item) for item in row['Marks'].split(',')]
dict_list.append(row)
return dict_list