Python文件索引器

Python文件索引器,python,Python,我的代码: def readData(fname): year = [] loc = [] with open(fname) as f: for line in f.read(): curr_line = line.split('\t') print(curr_line) year.append(curr_line[0]) loc.append(curr_li

我的代码:

def readData(fname):
    year = []
    loc = []

    with open(fname) as f:
        for line in f.read():
            curr_line = line.split('\t')
            print(curr_line)
            year.append(curr_line[0])
            loc.append(curr_line[1])

    return (year,loc)

def findLocation(yrList,locList,year):
   yrList = [str(yr) for yr in yrList]
   if year not in yrList:
       return "Not found."
   return locList[yrList.find(str(year))]

def main():
    yr, loc = readData('olympics.txt')
    print(yr)
    print(loc)
    x = input("Enter year: ")
    print(findLocation(yr,loc,x))
在完成主函数后,在输入年份后,它会给我这个错误

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    main()
  File "C:\Users\MyName\Downloads\Homework5.py", line 25, in main
    print(findLocation(yr,loc,x))
  File "C:\Users\MyName\Downloads\Homework5.py", line 18, in findLocation
    return locList[yrList.find(year)]
AttributeError: 'list' object has no attribute 'find'

有人能解释一下我的代码出了什么问题以及我为什么会出现这个错误吗?

写我的解决方案是为了回答问题,而不仅仅是为了给未来的访问者留下评论:


如果要查找列表中某个值的索引,可以使用yrList.indexyear,它将返回列表中元素的索引或抛出必须捕获的VALUERROR,而不是。查找不存在的问题。

请修复缩进,并将不属于问题的所有内容从文本墙中剥离。您需要告诉我们您实际在做什么,而不是您的家庭作业问题是什么。运行程序时会发生什么情况?如果遇到异常,我们确实需要查看异常的完整回溯。我们可能根本不需要知道作业。这是太多的信息,你能把它简化为与问题相关的部分吗?错误发生在哪一行?