samedi 23 mai 2020

I can't use random with CSV file

I need to import a CSV file and use random str in file CSV.

This is my code is error if run this is solution = list indices must be integers or slices, not str:

import csv
import random
with open('FOOD.csv')as csv_file:
    F=csv.DictReader(csv_file)
    f=list(F)
print(random.choice(f['Names']))

This is solution if I print(f):

[OrderedDict([('Names', 'break'), ('Cal', '50cal')]), OrderedDict([('Names', 'beer'), ('Cal', '180cal')]), OrderedDict([('Names', 'fishfried'), ('Cal', '380cal')]), OrderedDict([('Names', 'suki'), ('Cal', '240cal')]), OrderedDict([('Names', 'eeg'), ('Cal', '60cal')])]

Solution in my opinion is egg 60 cal or one thing in csvfile per run 1 time

What should I change?




Aucun commentaire:

Enregistrer un commentaire