Created
February 4, 2019 04:49
-
-
Save hmm01i/c4f1f097764e43f2aef868e8a2472e81 to your computer and use it in GitHub Desktop.
A snippet demonstrating working with data from a csv
This file contains hidden or 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
| #!/usr/bin/env python | |
| ### | |
| # This script loads a csv named intake.csv | |
| # For the purposes of the demo that file looked like this: | |
| # | |
| # name,weight,height,breakfast_foods,lunch_foods,diner_foods | |
| # jacob,155,6'2","beef,chicken",salad,beef | |
| # | |
| ### | |
| # this is a module that provides functionality for working with csvs | |
| import csv | |
| # "with" creates a context for working with a file. | |
| # and automatically closes the file when done | |
| with open('intake.csv', mode='r') as csv_file: | |
| print("") | |
| # loading the csv into a reader object that we can itererate over | |
| csv_reader = csv.DictReader(csv_file,delimiter=",") | |
| #loop through rows in the csv | |
| for row in csv_reader: | |
| # print the header for the breakfast | |
| print("== Breakfast options ==") | |
| # now loop through the breakfast items | |
| # the value gets stored in i | |
| for i in row['breakfast_foods'].split(','): | |
| # print the item | |
| print(i) | |
| # print a empty string to create an empty line for readability | |
| print("") | |
| # now do lunch stuff | |
| print("== Lunch options== ") | |
| # loop through the items | |
| for i in row['lunch_foods'].split(','): | |
| print(i) | |
| print("") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment