2016-03-22 12 views
0

私は比較的新しいPythonです。現在、リスト内の単語の位置とその文を構成する単語のリストを含むリストを使用する圧縮プログラムに取り組んでいます。これまで私は2つの関数の中にプログラムを書きました。 'compression'は、文章を構成する単語とそれらの単語の位置を取得します。私の2番目の関数は「再作成」と呼ばれ、この関数はリストを使用して文を再作成します。再作成されたセンテンスは、recreate.txtというファイルに保存されます。私の問題は、文章を構成する単語や単語の位置がそれぞれのファイルに書き込まれておらず、「再作成」ファイルが作成されていないことです。どんな助けでも大歓迎です。 それは、文を再作成しますが、私は、「再作成」ファイルをすることはありませんし、言葉で文を再作成しません再作成機能以外のすべての問題を修正しました を更新しましたおかげで:)テキストファイルとリストを使用して文を再作成する

sentence = input("Input the sentence that you wish to be compressed") 
sentence.lower() 
sentencelist = sentence.split() 
d = {} 
plist = [] 

wds = [] 
def compress():  
    for i in sentencelist: 
     if i not in wds: 
      wds.append(i) 
    for i ,j in enumerate(sentencelist): 
     if j in (d): 
      plist.append(d[j]) 
     else: 
      plist.append(i)  
    print (plist) 

    tsk3pos = open ("tsk3pos.txt", "wt")  

    for item in plist: 
     tsk3pos.write("%s\n" % item)  

    tsk3pos.close()      
    tsk3wds = open ("tsk3wds.txt", "wt")  

    for item in wds: 
     tsk3wds.write("%s\n" % item)  
    tsk3wds.close() 

    print (wds) 

def recreate(compress): 
    compress() 
    num = list() 
    wds = list() 
    with open("tsk3wds.txt", "r") as txt: 
     for line in txt: 
      words += line.split() 

    with open("tsk3pos.txt", "r") as txt: 
     for line in txt: 
      num += [int(i) for i in line.split()] 



    recreate = ' '.join(words[pos] for pos in num) 

    with open("recreate.txt", "wt") as txt: 
     txt.write(recreate) 

その位置で。

def recreate(compress): #function that will be used to recreate the compressed sentence. 
    compress() 
    num = list() 
    wds = list() 

    with open("words.txt", "r") as txt: #with statement opening the word text file 
     for line in txt: #iterating over each line in the text file. 
      words += line.split() #turning the textfile into a list and appending it to num 

    with open("tsk3pos.txt", "r") as txt: 
     for line in txt: 
      num += [int(i) for i in line.split()] 

    recreate = ' '.join(wds[pos] for pos in num) 

    with open("recreate.txt", "wt") as txt: 
     txt.write(recreate) 

    main() 



def main(): 
    print("Do you want to compress an input or recreate a compressed input?") 
    user = input("Type 'a' if you want to compress an input. Type 'b' if you wan to recreate an input").lower() 
    if user not in ("a","b"): 
     print ("That's not an option. Please try again") 
    elif user == "a": 
     compress() 
    elif user == "b": 
     recreate(compress) 
    main() 

main() 
+1

まずオフ、 'オープン=変数(ファイル)を使用していない' 'WITH'コンテキストマネージャを使用します。あなたは前後に切り替えているようです... – MattDMo

+0

あなたのお返事ありがとうございます。私はすべての変数= open(ファイル)を 'with'コンテキストマネージャに変更しました。私は自分のコードをチェックしましたが、まだ問題を選ぶことはできません。 @MattDMo – rmce

+0

「Enter code here」とは何ですか?表示していない他のコードはありますか?現在は関数を定義していますが、呼び出すことはありません。 – user234461

答えて

0

(まだ低効率)単純なアプローチ:

recreate_file_object = open ("C:/FullPathToWriteFolder/recreate.txt" , "w" ) 
recreate_file_object.write (recreate) 
recreate_file_object.close () 
+0

ご返信ありがとうございます。それは作成されていない再作成ファイルの問題を解決する必要がありますか? @RasikhJ – rmce

+0

それを試してみてください。わたしにはできる。 – RasikhJ

+0

ありがとうございました。私はそれに行くよ@RasikhJ – rmce

関連する問題