2017-06-07 68 views
0

現在、テキストファイルを読み込み、それをPythonシェルに印刷しようとしています。それは完璧にファイルを読み込みますが、読み込んだ行を別々に読み出すと、AttributeErrorがあることがわかります。どうすればこの問題を解決できますか?AttributeError: 'list'オブジェクトに属性 'readlines'がありません(Python 3.4.4)

は、ここに私のコードです:

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("Searching for fixtures...") 
    time.sleep(3) 
    data = [line.strip() for line in open("Y:/Computing & Business/Students/Computing/Year 10/CA 2017 Edexcel/firesideFixtures.txt").readlines()] 
    lines = data.readlines() 
    print(data) 
    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("~~incomplete~~") 
elif menuOptions == 'C': 
    print("~~incomplete~~") 
elif menuOptions == 'Q': 
    print("Exiting Applicaion...") 
    time.sleep(2) 
    exit() 

そして、これは私が受け取るものです:あなたがリストにreadlinesを適用しようとしている

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 
Searching for fixtures... 
Traceback (most recent call last): 
    File "E:\Python\Python Work\League\League3.py", line 20, in <module> 
    lines = data.readlines() 
AttributeError: 'list' object has no attribute 'readlines' 
+0

オープン(R "Yのラインのために、'データ= [line.strip()としてdata' 'の割り当てを交換してみてください:\コンピューティング&ビジネス\ Students \ Computing \ Year 10 \ CA 2017 Edexcel \ firesideFixtures.txt ")。readlines()]'。 – Gahan

+0

ありがとうございますが、これは結果には何の差もなく、以前と同じように印刷されます。 – Foxy

答えて

0

。あなたの例では、dataは既にファイル行を含むリストです。行:

lines = data.readlines() 

は冗長です。削除することができます。さらに、方法readlines()は、readline()を使用してEOFまで読み取ります。は、その行を含むリストを返します。

より、私はあなたがファイルを操作するときwith()を使用することをお勧め:

with open(some_file) as f: 
    .... 

with文を使用する利点は、ファイル(あなたがやらなかったもの)に関係なく閉じることが保証されていることですネストされたブロックの終了方法ブロックの最後の前に例外が発生すると、例外が外部例外ハンドラによってキャッチされる前にファイルを閉じます。あなたの印刷要求を1として


、単にリストをループし、それをプリントアウト:

for line in data: 
    print(line) 

を私はあなたが公式Python documentationを読むことをお勧めします。

+0

ありがとうございます。 :D – Foxy

関連する問題