2016-04-10 29 views
-5

私のコードには、圧縮された文章が含まれているファイル "filefile.txt"があります。ファイルは次のように配置されています:ファイルを解凍する方法

1 
2 
3 
4 
5 
1 
2 
6 
9 
10 
11 
2 
12 
12 
9 

This 
is 
a 
sentence 
. 
too 
! 
Yo 
yo 
bling 

解凍したい元のテキストに「!」と書かれています。

私のコードは言う:これは誤りである

fo = open("filefile.txt","r") 
script = fo.readline() 
script2 = fo.readline() 
fo.close() 
script2 = script2.split() 
script = [s.strip("\n") for s in script] 

sentencewords = [] 

while len(script) > 0: 
    for p in script: 
     sentencewords.append(enumerate(script2.index(p))) 
     script.remove(0) 

print(sentencewords) 

:私は含まれてsentencewordsが必要

Traceback (most recent call last): 
    File "F:\Computing code attempts\AT13.py", line 46, in <module> 
    sentencewords.append(enumerate(script2.index(p))) 
ValueError: '1' is not in list 

"これは文であるこれは、あまりにもあるヨーヨーケバケバしい!"

今変更しましたが、まだ動作しません。 sentencewords.append(列挙中: "\計算コードは\ AT13.pyを試行F" は、ライン46、(script2.enumerate sentencewords.append(列挙(script2.enumerate(P)))

'Traceback (most recent call last): 

ファイル(p))) AttributeError: 'list'オブジェクトに '列挙型'属性がありません

この問題を回避する別の方法があるか、現在のコードを修正する方法があるか知りませんか?

fo = open("filefile.txt","r") 
script = fo.readline() 
script2 = fo.readline() 
fo.close() 
script2 = script2.split() 
script = [s.strip("\n") for s in script] 

sentencewords = [] 

indexes = [] 
for line in fo: 
    if line.strip().isdigit(): 
     indexes.append(line) 
    else: 
     break 


words = [line.strip() for line in fo if line.strip()] 

while len(script) > 0: 
    for p in script: 
     sentencewords.append(words[index-1]) 


print(sentencewords) 

コードは更新されましたが、Pythonの最新の出力でI/Oが何を意味するのか分かりません。私のコードを修正する方法について

Traceback (most recent call last): 
    File "F:/Computing code attempts/attempt14.py", line 45, in <module> 
    for line in fo: 
ValueError: I/O operation on closed file. 

任意の提案、私は、ファイルが近くにありながら、あなたのコードは、ファイルの解析を使用しています

+0

どのようなエラーは起こりますか? – vaultah

+0

あなたの質問には質問の内容を示すタイトルを付けてください。 – khelwood

+0

http://meta.stackexchange.com/a/10812/312450 – Paul

答えて

0

に感謝するだろう:

fo.close() 
... 
indexes = [] 
for line in fo: 

あなたはfo.close()を遅らせることができますあなたのスクリプトの最後にそれが渡されますValueError: I/O operation on closed file.

関連する問題