File tree 2 files changed +21
-1
lines changed
2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change 34
34
4 . [ Tuple] ( https://github.com/antoniolopez7217/Python_Programming_MOOC_2023_Introduction/tree/main/part5/4.%20Tuple )
35
35
36
36
### [ Part 6] ( https://github.com/antoniolopez7217/Python_Programming_MOOC_2023_Introduction/tree/main/part6 )
37
- 1 . Reading files
37
+ 1 . [ Reading files] ( https://github.com/antoniolopez7217/Python_Programming_MOOC_2023_Introduction/tree/main/part6/1.%20Reading%20files )
38
38
2 . Writing files
39
39
3 . Handling errors
Original file line number Diff line number Diff line change
1
+ # The file fruits.csv contains names of fruits, and their prices, in the format specified in this example:
2
+
3
+ # banana;6.50
4
+ # apple;4.95
5
+ # orange;8.0
6
+ # ...etc...
7
+ # Please write a function named read_fruits, which reads the file and returns a dictionary based on the contents.
8
+ # In the dictionary, the name of the fruit should be the key, and the value should be its price. Prices should be of type float.
9
+
10
+ # NB: the function does not take any arguments.
11
+ # The file you are working with is always named fruits.csv.
12
+
13
+ def read_fruits ():
14
+ fruits_dict = {}
15
+ with open ("fruits.csv" ) as new_file :
16
+ for line in new_file :
17
+ line = line .replace ("\n " ,"" )
18
+ parts = line .split (";" )
19
+ fruits_dict [parts [0 ]] = float (parts [1 ])
20
+ return fruits_dict
You can’t perform that action at this time.
0 commit comments