TypeError:list indices必须是整数,而不是_io.TextIOWrapper

时间:2017-06-07 11:16:04

标签: python-3.x

我正在尝试在文本文档中搜索某个数字。它读取文件很好,以后使用相同的代码可以打印正常,但是当我告诉它在文本文档中找到某个数字时它会告诉我TypeError: list indices must be integers, not _io.TextIOWrapper。我四处寻找,但所有问题都说“不是str'而我的说法不是_io.TextIOWrapper'

这是我的代码(我正在沿着路线' A'在菜单选项下'

import time
import linecache
print("Welcome to the League Fixture Manager!")
time.sleep(3)
print("What would you like to do?")
time.sleep(1)
print("Press A to search for a fixture.")
time.sleep(0.1)
print("Press B to view Outstanding fixtures.")
time.sleep(0.1)
print("Press C to display the leader board")
time.sleep(0.1)
print("Or press Q to quit, this will exit the application.")
time.sleep(0.1)
menuOptions = input("What would you like to do? A, B, C, or Q.")
if menuOptions == 'A':
    print("Please enter the fixture number you are looking for... ")
    fixtureQuestion = input(int())
    fixtureQuestion = int(fixtureQuestion)
    fixtureQuestion = fixtureQuestion - (1)
    time.sleep(3)
    if fixtureQuestion < 1:
        print("This fixture is not available, please re-run the application...")
        time.sleep(2)
        exit()
    elif fixtureQuestion > 190:
        print("This fixture is not available, please re-run the application...")
        time.sleep(2)
        exit()
    else:
        searchData = open("Y:\Computing & Business\Students\Computing\Year 10\CA 2017 Edexcel\\firesideFixtures.txt")
        lines = searchData.readlines()
        print(lines[searchData])
        time.sleep(1)
        print("Return to menu?")
        menuReturn = input("Y or N")
        if menuReturn == 'Y':
            print("Press B to view outstanding fixtures.")
            time.sleep(0.1)
            print("Press C to display the leaderboard")
            time.sleep(0.1)
            print("Or press Q to exit the application.")
            time.sleep(0.1)
            print("You cannot review the fixture list now you have seen it however you can scroll up to view it again.")
            time.sleep(0.1)
            menuOptions2 = input("What would you like to do? B, C, or Q?")
            if menuOptions2 == 'B':
                print("~~incomplete~~")
            elif menuOptions2 == 'C':
                print("~~incomplete~~")
            elif menuOptions2 == 'Q':
                print("Exiting Application...")
                time.sleep(1)
                exit()
        elif menuReturn == 'N':
            print("Exiting Application...")
            time.sleep(2)
            exit()
elif menuOptions == 'B':
    print("Searching for fixtures...")
    time.sleep(3)
    data = [line.strip() for line in open(r"Y:\Computing & Business\Students\Computing\Year 10\CA 2017 Edexcel\firesideFixtures.txt").readlines()]
    for line in data:
        print(line)
    time.sleep(1)
    print("Return to menu?")
    menuReturn = input("Y or N")
    if menuReturn == 'Y':
        print("Press A to search for a fixture")
        time.sleep(0.1)
        print("Press C to display the leaderboard")
        time.sleep(0.1)
        print("Or press Q to exit the application.")
        time.sleep(0.1)
        print("You cannot review the fixture list now you have seen it however you can scroll up to view it again.")
        time.sleep(0.1)
        menuOptions2 = input("What would you like to do? B, C, or Q?")
        if menuOptions2 == 'A':
            fixtureQuestion = input(int("Please enter the fixture number you are looking for... "))
            fixtureQuestion = fixtureQuestion - 1
            time.sleep(3)
            if fixtureQuestion < 1:
                print("This fixture is not available, please re-run the application...")
                time.sleep(2)
                exit()
            elif fixtureQuestion > 190:
                print("This fixture is not available, please re-run the application...")
                time.sleep(2)
                exit()
            else:
                searchData = open(r"Y:\Computing & Business\Students\Computing\Year 10\CA 2017 Edexcel\firesideFixtures.txt").readlines()
                lines = searchData.readlines()
                print(lines[fixtureQuestion])
                time.sleep(1)
                print("Return to menu?")
                menuReturn = input("Y or N")
                if menuReturn == 'Y':
                    print("Press C to display the leaderboard")
                    time.sleep(0.1)
                    print("Or press Q to exit the application.")
                    time.sleep(0.1)
                    print("You cannot review the fixture list now you have seen it however you can scroll up to view it again.")
                    time.sleep(0.1)
                    menuOptions2 = input("What would you like to do? B, C, or Q?")
                    if menuOptions2 == 'C':
                        print("~~incomplete~~")
                    elif menuOptions2 == 'Q':
                        print("Exiting Application...")
                        time.sleep(1)
                        exit()
                elif menuReturn == 'N':
                    print("Exiting Application...")
                    time.sleep(2)
                    exit()
                elif menuOptions2 == 'Q':
                    print("Exiting Application...")
                    time.sleep(1)
                    exit()
elif menuOptions == 'C':
    while RetryForC == "Yes":
        RetryForC == "No"
        fireRead = open("Y:\Computing & Business\Students\Computing\Year 10\CA 2017 Edexcel\firesideResults.txt")
        for line in fireRead:
            fireRead = open("Y:\Computing & Business\Students\Computing\Year 10\CA 2017 Edexcel\firesideResults.txt")
            InfoOne = line.split(',')[0]
            InfoTwo = line.split(',')[1]
            InfoThree = line.split(',')[2]
            InfoFour = line.split(',')[3]
            PointCounter = int(line.split(',')[2])
            PointCounter = PointCounter * 3
            fireRead.close()
            print("Player:",InfoOne,"Has played:",InfoTwo,", has won:",InfoThree,", has lost:",InfoFour,", and therefore has",PointCounter,"many points.")
        print("Retry?")
        RestForC = str(input("Yes/No  "))
    print("The program will now close...")
    time.sleep(5)
    exit()
elif menuOptions == 'Q':
    print("Exiting Applicaion...")
    time.sleep(2)
    exit()

这是我的结果:

Welcome to the League Fixture Manager!
What would you like to do?
Press A to search for a fixture.
Press B to view Outstanding fixtures.
Press C to display the leader board
Or press Q to quit, this will exit the application.
What would you like to do? A, B, C, or Q.A
Please enter the fixture number you are looking for... 
016
Traceback (most recent call last):
  File "E:\Python\Python Work\League\League3.py", line 33, in <module>
    print(lines[searchData])
TypeError: list indices must be integers, not _io.TextIOWrapper

我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:-1)

这一行:

print(lines[searchData])

没有任何意义。列表的索引必须是整数,因此您可以这样做:

>>> my_list = [1, 2, 3]
>>> my_list[0]
1

lines是一个包含文件行的列表,search_data是一个文件对象。看起来你似乎缺少一些基本的python概念。请仔细阅读我在10分钟前提出的其他问题中粘贴的文档!