Skip to content

Commit d577bd4

Browse files
committed
using DictReader, kind of.
1 parent f890d33 commit d577bd4

File tree

2 files changed

+75
-301
lines changed

2 files changed

+75
-301
lines changed

cookbook.py

+26-24
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,45 @@
22
import csv
33

44

5-
html = "<body>"
5+
html = "<body>\n"
66

77
# opening in a way that will close the file when we are done
8-
with open("recipes.csv", "rU") as csvfile:
9-
# reading file
10-
reader = csv.reader(csvfile)
8+
#with open("recipes.csv", "rb") as csvfile:
119

12-
rownum = 0
13-
for row in reader:
14-
# Save header row.
15-
if rownum == 0:
16-
header = row
17-
if rownum == 1:
18-
colnum = 0
19-
for col in row:
20-
print '%s: %s' % (header[colnum], col)
21-
colnum += 1
10+
# reading file
2211

12+
file_name = 'recipes.csv'
13+
delimiter = ','
14+
quote_character = '"'
2315

16+
csv_recipes = open(file_name, 'rU')
17+
csv_reader = csv.DictReader(csv_recipes, fieldnames=[], restkey='undefined-fieldnames', delimiter=delimiter, quotechar=quote_character)
2418

19+
current_row = 0
2520

26-
#else:
27-
# colnum = 0
28-
# for col in row:
29-
# print '%s: %s' % (header[colnum], col)
30-
# colnum += 1
21+
for row in csv_reader:
22+
current_row += 1
3123

32-
rownum += 1
24+
# Use heading row as field names for all other rows
25+
if current_row == 1:
26+
csv_reader.fieldnames = row['undefined-fieldnames']
27+
continue
28+
if current_row > 1:
29+
html = html + "<section>\n <ul>\n"
3330

31+
for data in row:
32+
html = html + "<li>" + data + "</li>\n" #how do I describe value in key value pairs?
33+
#possibly list out every row['Title'] as an option and then if None, etc. should be able to loop through all of them generically though.
3434

35-
html = html + "div"
36-
#for data in row:
37-
# html = html + "<td>" + data + "</td>"
35+
html = html + "</ul>\n" + "</section\n"
36+
37+
print(html)
3838

39+
#print(row['Title'] + '/' + row['Directions'])
3940

40-
html = html +"</div>\n"
4141

42+
#for data in row:
43+
# html = html + "<td>" + data + "</td>"
4244

4345

4446

0 commit comments

Comments
 (0)