Last active
August 29, 2015 14:00
-
-
Save bbengfort/11307306 to your computer and use it in GitHub Desktop.
Simple (incorrect) parsing of a csv file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Food | Measure | Weight (g) | kCal | Fat (g) | Carbo(g) | Protein (g) | |
---|---|---|---|---|---|---|---|
Source:USDA Nutrient Database for Standard Reference | Release 12 | Pocket v1.1 | www.nal.usda.gov/fnic/foodcomp | ||||
1000 Island,Salad Drsng,Local | 1 Tbsp | 15 | 25 | 2 | 2 | 0 | |
1000 Island,Salad Drsng,Reglr | 1 Tbsp | 16 | 60 | 6 | 2 | 0 | |
40% Bran Flakes,Kellogg's | 1 oz | 28.35 | 90 | 1 | 22 | 4 | |
40% Bran Flakes,Post | 1 oz | 28.35 | 90 | 0 | 22 | 3 | |
Alfalfa Seeds,Sprouted,Raw | 1 Cup | 33 | 10 | 0 | 1 | 1 | |
All-Bran Cereal | 1 oz | 28.35 | 70 | 1 | 21 | 4 | |
Almonds,Slivered | 1 Cup | 135 | 795 | 70 | 28 | 27 | |
Almonds,Whole | 1 oz | 28.35 | 165 | 15 | 6 | 6 | |
Angelfood Cake,From Mix | 1 Cake | 635 | 1510 | 2 | 342 | 38 | |
Angelfood Cake,From Mix | 1 Piece | 53 | 125 | 0 | 29 | 3 | |
Apple Juice,Canned | 1 Cup | 248 | 115 | 0 | 29 | 0 | |
Apple Pie | 1 Pie | 945 | 2420 | 105 | 360 | 21 | |
Apple Pie | 1 Piece | 158 | 405 | 18 | 60 | 3 | |
Apples,Dried,Sulfured | 10 Rings | 64 | 155 | 0 | 42 | 1 | |
Apples,Raw,Peeled,Sliced | 1 Cup | 110 | 65 | 0 | 16 | 0 | |
Apples,Raw,Unpeeled,2 Per Lb | 1 Apple | 212 | 125 | 1 | 32 | 0 | |
Apples,Raw,Unpeeled,3 Per Lb | 1 Apple | 138 | 80 | 0 | 21 | 0 | |
Applesauce,Canned,Sweetened | 1 Cup | 255 | 195 | 0 | 51 | 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def parse_file(path): | |
with open(path, 'rb') as f: | |
header = f.readline().split(",") | |
for line in f: | |
fields = line.split(",") | |
entry = {} | |
for i, val in enumerate(fields): | |
entry[header[i].strip()] = val.strip() | |
yield entry | |
if __name__ == "__main__": | |
for row in parse_file('calories.csv'): | |
print row | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unicodecsv as csv | |
def parse_file(path): | |
numeric_fields = ('Weight (g)','kCal','Fat (g)','Carbo(g)','Protein (g)') | |
with open(path, 'rb') as f: | |
reader = csv.DictReader(f, delimiter=',', quotechar='"') | |
for idx, row in enumerate(reader): | |
if idx == 0: continue | |
for field in numeric_fields: | |
row[field] = float(row[field]) | |
yield row | |
if __name__ == '__main__': | |
for row in parse_file('calories2.csv'): | |
print row |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment