Home > OS >  string indices must be integers in a defaultdict, CSV File
string indices must be integers in a defaultdict, CSV File

Time:04-30

I have .csv file, about ramen and brands, varieties and ratings. I want to figure out, which Brand uses the Variety "Tom Yum" the most. I tried it with a defaultdict but i get the error code: string indices must be integers

This is my code so far:

from collections import defaultdict
tomyum = []

for row in liste:
    if "Tom Yum" in row["Variety"]:
        tomyum.append(row["Brand"])
        


d = defaultdict(int)

for row in tomyum:
    for brand in row['Brand']:
        d[brand]  = 1
d   

Anyone any Ideas?

CodePudding user response:

tomyum is a list of strings. Hence, in the following loop: for row in tomyum, row represents a string. That's why you cannot do row['Brand'] in the next line, since you can access only indices in row, i.e. row[0], row[1], etc.

  • Related