Python:列表索引必须是整数,而不是str?

oce*_*800 0 python csv list

所以我的代码看起来像这样:

with open(completeCSV, 'r') as csvfile:
    test = csv.reader(csvfile, delimiter=';')
    for line in test:
        print(line)
        i = i + 1  # on the first line, i will equal 1
        count = line[0]
        if count == '1':
            for prof in proflist:  # vertex1=prof
                if line[1].lower() == proflist[prof]:
                    # if vertex1 is a professor, you want to keep the edge.
                    lines_to_keep.append(i)
                    break  # breaks and doesn't check the rest of profs
Run Code Online (Sandbox Code Playgroud)

它基本上读取CSV,并检查来自csv的值是否等于列表中的另一个值proflist.

我收到这个错误:

回溯(最近一次调用最后一次):文件"C:/Users/sskadamb/PycharmProjects/BetterDelimiter/filter.py",第50行,如果行[1] .lower()== proflist [prof]:TypeError:list indices必须是整数,而不是str

是因为proflist[prof]?但我想查看所有proflist反对的条目 line[1].我该怎么做,我做错了什么?我可以不迭代这样的列表吗?

Pyn*_*hia 5

prof是一个元素proflist,而不是索引.

替代

if line[1].lower()==proflist[prof]:
Run Code Online (Sandbox Code Playgroud)

if line[1].lower() == prof:
Run Code Online (Sandbox Code Playgroud)