私はコードに熟練していないと私のGCSEコンピュータサイエンス制御の評価に関する質問があります。私はかなり遠くになっています、それはこの最後のハードルが私を抱きしめているだけです。 この作業では、以前に作成した単純なファイル圧縮システムを使用する必要があります。また、「点字を含む複数の文章でテキストファイルを圧縮するプログラムを開発する必要があります。元のファイルを再作成するための単語リストと位置リストを作成することができます。また、圧縮ファイルを作成し、元のファイルの句読点や大文字を含む全文を再作成することができます。私はGCSEコンピュータサイエンスのための簡単なファイル圧縮システムを作成する必要があります
これまでのところ、私は私の最初のプログラムでテキストファイルとして、すべてを格納することが可能となっています
sentence = input("Enter a sentence: ")
sentence = sentence.split()
uniquewords = []
for word in sentence:
if word not in uniquewords:
uniquewords.append(word)
positions = [uniquewords.index(word) for word in sentence]
recreated = " ".join([uniquewords[i] for i in positions])
print (uniquewords)
print (recreated)
positions=str(positions)
uniquewords=str(uniquewords)
positionlist= open("H:\Python\ControlledAssessment3\PositionList.txt","w")
positionlist.write(positions)
positionlist.close
wordlist=open("H:\Python\ControlledAssessment3\WordList.txt","w",)
wordlist.write(uniquewords)
wordlist.close
これは、リストにすべてのものを作り、それを書き込むことが可能であるように、文字列に変換しテキスト文書に変換する。さて、問題はどこにあるプログラム番号2がある:私がしようとすると、このコードを実行すると
uniquewords=open("H:\Python\ControlledAssessment3\WordList.txt","r")
uniquewords= uniquewords.read()
positions=open("H:\Python\ControlledAssessment3\PositionList.txt","r")
positions=positions.read()
positions= [int(i) for i in positions]
print(uniquewords)
print (positions)
recreated = " ".join([uniquewords[i] for i in positions])
FinalSentence=
open("H:\Python\ControlledAssessment3\ReconstructedSentence.txt","w")
FinalSentence.write(recreated)
FinalSentence.write('\n')
FinalSentence.close
、このエラーが表示されます。
Traceback (most recent call last):
File "H:\Python\Task 3 Test 1.py", line 7, in <module>
positions= [int(i) for i in positions]
File "H:\Python\Task 3 Test 1.py", line 7, in <listcomp>
positions= [int(i) for i in positions]
ValueError: invalid literal for int() with base 10: '['
だから、どのように私は2番目のプログラムを再コンパイルするために得ると思います文章の本文に?ありがとうございました。これが長すぎる投稿であればごめんなさい、私は永遠にこの作業をしようとしました。 これは、ブラケット、コンマ、スペースなどを含む文字列に変換されたリストと関係があると思いますので、両方の文字列を元の状態に戻して文を再作成する方法はありますか?ありがとう。
ありがとう、私はこれを明日利用しようとします。これははるかに理にかなっています。 :) – Curtis