-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanuplating_csv_file2.py
More file actions
42 lines (35 loc) · 1.19 KB
/
manuplating_csv_file2.py
File metadata and controls
42 lines (35 loc) · 1.19 KB
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
import os
import csv
'''
Using the CSV file of flowers again, fill in the gaps of the
contents_of_file function to process the data without turning
it into a dictionary. skip over the header record with the
field names.
'''
# 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 file:
# Read the rows of the file
rows = csv.reader(file)
headers = next(rows)
# Process each row
for row in rows:
name,color,types = row
# Format the return string for data rows only
return_string += "a {} {} is {}\n".format(color,name,types)
return return_string
#Call the function
print(contents_of_file("flowers.csv"))