Created
October 26, 2020 12:53
-
-
Save pandorica-opens/6d81943526aa3c12da740b11eb7edec1 to your computer and use it in GitHub Desktop.
Process the flowers data again without turning it into a dictionary.
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
| import os | |
| import csv | |
| # Create a file with data in it | |
| def create_file(filename): | |
| with open(filename, "w") as file: | |
| file.write("name,color,type\n") | |
| file.write("carnation,pink,annual\n") | |
| file.write("daffodil,yellow,perennial\n") | |
| file.write("iris,blue,perennial\n") | |
| file.write("poinsettia,red,perennial\n") | |
| file.write("sunflower,yellow,annual\n") | |
| # Read the file contents and format the information about each row | |
| def contents_of_file(filename): | |
| return_string = "" | |
| # Call the function to create the file | |
| create_file(filename) | |
| # Open the file | |
| with open(filename, 'r') as f: | |
| # Read the rows of the file | |
| rows = csv.reader(f) | |
| # Skips the headers | |
| next(rows) | |
| # Process each row | |
| for row in rows: | |
| name, color, typeflower = row | |
| # Format the return string for data rows only | |
| return_string += "a {} {} is {}\n".format(color, name, typeflower) | |
| return return_string | |
| #Call the function | |
| print(contents_of_file("flowers.csv")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Error on line 31:
return_string += "a {} {} is {}\n".format(row["color"], row["name"], row["type"])
^
IndentationError: unexpected indent