2017-04-27 8 views
2

私はコードに熟練していないと私の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番目のプログラムを再コンパイルするために得ると思います文章の本文に?ありがとうございました。これが長すぎる投稿であればごめんなさい、私は永遠にこの作業をしようとしました。 これは、ブラケット、コンマ、スペースなどを含む文字列に変換されたリストと関係があると思いますので、両方の文字列を元の状態に戻して文を再作成する方法はありますか?ありがとう。

答えて

1

まず、ポジションをリテラル文字列として保存するのは非常に奇妙です。各要素を保存する必要があります(ユニークワードと同じです)。これを考慮して、何かのように:

program1.py:

sentence = input("Type sentence: ") 

# this is a test this is a test this is a hello goodbye yes 1 2 3 123 

sentence = sentence.split() 
uniquewords = [] 
for word in sentence: 
    if word not in uniquewords: 
     uniquewords.append(word) 

positions = [uniquewords.index(word) for word in sentence] 

with open("PositionList.txt","w") as f: 
    for i in positions: 
      f.write(str(i)+' ') 

with open("WordList.txt","w") as f: 
    for i in uniquewords: 
      f.write(str(i)+' ') 

program2.py:

with open("PositionList.txt","r") as f: 
    data = f.read().split(' ') 
positions = [int(i) for i in data if i!=''] 

with open("WordList.txt","r") as f: 
    uniquewords = f.read().split(' ') 

sentence = " ".join([uniquewords[i] for i in positions]) 
print(sentence) 

PositionList.txt

0 1 2 3 0 1 2 3 0 1 2 4 5 6 7 8 9 10 

WordList.txt

this is a test hello goodbye yes 1 2 3 123 
+0

ありがとう、私はこれを明日利用しようとします。これははるかに理にかなっています。 :) – Curtis

関連する問題