私のコードには圧縮された文章が含まれているファイル "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:\code attempts\AT13.py", line 46, in <module>
sentencewords.append(enumerate(script2.index(p)))
ValueError: '1' is not in list
"これは文であるこれは、あまりにもあるヨーヨーケバケバしい!"
今変更しましたが、まだ動作しません。 sentencewords.append((script2.enumerate(P))を列挙する)
'Traceback (most recent call last):
ファイル: sentencewords.appendに、ライン46 "F \コード\ AT13.pyを試みる"(((script2.enumerateを列挙する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:/code attempts/attempt14.py", line 45, in <module>
for line in fo:
ValueError: I/O operation on closed file.
fo.closeは()のコードのさらに下に移動されている今では私のコードを修正する方法について
Traceback (most recent call last):
File "F:\code attempts\attempt14.py", line 55, in <module>
sentencewords.append(words[index-1])
MemoryError
任意の提案を言い、私は
感謝に感謝するだろう
ループする前にすでにファイル "fo"を閉じているため、このエラーが発生します。 – TheLazyScripter
........ありがとうございました...... –