2017-06-07 13 views
-1

テキストドキュメント内の特定の番号を検索しようとしています。それは細かいファイルを読み取り、後で同じコードを使用して印刷することはできますが、テキスト文書で特定の番号を見つけるように指示すると、TypeError: list indices must be integers, not _io.TextIOWrapperと表示されます。私はすべての周りを見回したが、鉱山は言うのに対し、すべての質問には、「STRない」と言う「ではない_io.TextIOWrapper」TypeError:リストインデックスは、_io.TextIOWrapperではなく整数でなければなりません。

をこれが私のコードである(I「」「はmenuOptions」

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

このライン:

print(lines[searchData]) 

意味がありません。リストのインデックスは整数でなければなりませんので、あなたが行うことができます。

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

linesは、ファイルの行が含まれており、search_dataはファイルオブジェクトであるリストです。いくつかの基本的なPythonの概念が欠落しているようです。 10分前に質問した他の質問に貼り付けたドキュメントを読んでください!

+0

返事ありがとうございました(そして私は本当にばかげているとは申し訳ありません...)私はあなたが行について貼り付けたドキュメントを読んだだけですが、それをテキストファイルから読み込んで印刷することについて何も言いません。私はあなたのコードをコピーしましたが、 'my_List [0] NameError:name 'my_List'は定義されていません'私はそれが定義されていないと言っているのは分かっていますが、それはドキュメンテーションのそれについて何も言わない – Foxy

関連する問題