How to return a dictionary | Python
How to return a dictionary | Python
Whats going on is that youre returning right after the first line of the file doesnt match the id youre looking for. You have to do this:
def query(id):
for line in file:
table = {}
(table[ID],table[name],table[city]) = line.split(;)
if id == int(table[ID]):
file.close()
return table
# ID not found; close file and return empty dict
file.close()
return {}
def query(id):
for line in file:
table = line.split(;)
if id == int(table[0]):
yield table
id = int(input(Enter the ID of the user: ))
for id_, name, city in query(id):
print(ID: + id_)
print(Name: + name)
print(City: + city)
file.close()
Using yield..
How to return a dictionary | Python
def prepare_table_row(row):
lst = [i.text for i in row if i != un]
return dict(rank = int(lst[0]),
grade = str(lst[1]),
channel=str(lst[2])),
videos = float(lst[3].replace(,, )),
subscribers = float(lst[4].replace(,, )),
views = float(lst[5].replace(,, )))