-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path27. reading CSV files.py
75 lines (47 loc) · 1.44 KB
/
27. reading CSV files.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'''
Next up, let us cover the reading of CSV files into memory.
One of the more popularly requested file types for reading into
memory is csv files, so let us cover them.
here I will just create a simple example.csv file
'''
import csv
with open('example.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
for row in readCSV:
print(row)
print(row[0])
print(row[0],row[1],row[2],)
#### next part....
import csv
with open('example.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
dates = []
colors = []
for row in readCSV:
color = row[3]
date = row[0]
dates.append(date)
colors.append(color)
print(dates)
print(colors)
#### now you want to maybe ask some questions with data...
# what day was the color green let's ask?
import csv
with open('example.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
dates = []
colors = []
for row in readCSV:
color = row[3]
date = row[0]
dates.append(date)
colors.append(color)
print(dates)
print(colors)
# now, remember our lists?
whatColor = input('What color do you wish to know the date of?:')
coldex = colors.index(whatColor)
theDate = dates[coldex]
print('The date of',whatColor,'is:',theDate)
#but..................... what if we enter something that is non-existent?
# oh noes, an error!