0

I've been trying to make a takeaway directory of sorts that will allow you to search by type of food, rating, cost etc.

I have been having a bit of trouble getting more than one row to print. If I am searching for say Pizza, Slice of heaven will come up like this:

-Slice of Heaven,Pizza,4.1/5,01904 731377,

but what im aiming for is:

-Slice of Heaven,Pizza,4.1/5,01904 731377
-Salt and Pepper,Pizza,3.0/5,01904 733733
-Bodrum Pizza,Pizza,3.3/5,01904 673300
-Dominoes Pizza,Pizza,3.6/5,01904 690000

The csv file looks like this:

Name,Type of food,Rating,Phone Number,,,
Fish King Takeaway ,Fish and Chips/Chinese ,3.9/5,01904 659775,,,
Heworth Chinese ,Chinese,3.9/5,01904 415163,,,
Clifton Chinese,Chinese,2.8/5,01904 655025,,,
Jaipur Spice,Curry,4.1/5,01904 673550,,,
Lukes Takeaway,Pizza,4.7/5,01904 610011,,,
Efes Pizza,Pizza,3.6/5,01904 652210,,,
Jorvik Spice,Curry,4.3/5,01904 624040,,,
Lucky Dragon ,Chinese,3.0/5,01904 782002,,,
Salt and Pepper,Pizza,3.0/5,01904 733733,,,
Bodrum Pizza,Pizza,3.3/5,01904 673300,,,
Dominoes Pizza,Pizza,3.6/5,01904 690000,,,
Slice of Heaven,Pizza,4.1/5,01904 731377,,,

and this is my code:

import csv
import random
    
f = open('Takeaway finder/Takeaway.csv', 'r')
reader = csv.reader(f)
takeaway = {}

for row in reader:
    takeaway[row[1]] = {'Name' :row[0],'Rating': row[2], 'Phone Number': row[3]}

choice = input("what do you fancy tonight?")

print(takeaway[choice])
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 1
    `takeaway` only saves the last row it saw of each category...You should save it as a list. But if possible, it would be much easier to these things in `pandas` – Tomerikoo Jul 21 '21 at 09:34

3 Answers3

0

I think our problem arises when you assign a unique value to the takeaway dict.

If you have, for example two rows like the followings:

AAAA, Pizza, 9, 99999999
BBBB, Pizza, 10, 999999998

First it will do takeaway[Pizza] = AAAA, 9, 99999999 and when it encounters the second row it will overwrite the previous line. (Which seems not de desired behaviour)

Possible solution: Instead of setting the value of takeaway[pizza], you can append them. Simply change:

takeaway[row[1]] = {'Name' :row[0],'Rating': row[2], 'Phone Number': row[3]}

for

takeaway[row[1]].append({'Name' :row[0],'Rating': row[2], 'Phone Number': row[3]})

Note: Take into consideration that the default dict object in Python does not allow such type of operations (you cannot append to a an empty object). So, instead, you can use the library collections this way:

from collections import defaultdict
takeaway = defaultdict(list) #Each entry of the takeaway dict is now an empty list
takeaway[key].append(dictionary_with_data) #Now you can append the dictionaries to this empty list
0

The problem is unrelated to your CSV file; the problem is that you read it into a dict where you only save one provider per category.

The obvious solution is to make the dict store a list of providers for each category. A common arrangement is to use defaultdict so you don't have to separately check whether the current key already exists.

import csv
# import random  # unused import
from collections import defaultdict

takeaway = defaultdict(list)

with open('Takeaway finder/Takeaway.csv', 'r') as f:
    for row in csv.reader(f):
        takeaway[row[1]].append({'Name' :row[0],'Rating': row[2], 'Phone Number': row[3]})

choice = input("what do you fancy tonight?")

print(takeaway[choice])
tripleee
  • 175,061
  • 34
  • 275
  • 318
-1

You can use iteritems

dict((key,value) for key, value in takeaway.iteritems() if key == choice)
Rizquuula
  • 578
  • 5
  • 15
  • Can you explain this answer? `takeaway` is the result dict. How can you iterate over it in order to create it? – Tomerikoo Jul 21 '21 at 10:02