2017-02-07 12 views
0

私がやっている課題については、私はロックされたPDFファイルを解読する必要があります。私はそうするためにパスワードリストを作成しようとしていますが、このコードが生成する結果をテキストファイルに出力する仕方は解りません。どのように出力結果、このコードは、外部のテキストファイルに作成し、誰かが私を見ることができれば、私はこれまで働いていない試したテキストファイルPython 2.7に出力

from random import shuffle 
with open('randomwords.txt', 'r') as data: 
    data = data.read().split() 
    while(True): 
     shuffle(data) 
     password = '' 
     for x in data[:3]: 
      password += x 
     print password.replace('o','0') 

すべては、いただければ幸いです。

fucntionを変換:

def transform(word): 
    from random import shuffle 
    with open('randomwords.txt', 'r') as data: 
      data = data.read().split() 
     while(True): 
       shuffle(data) 
       password = '' 
       for x in data[:3]: 
         password += x 
       print password.replace('o','0') 
    return word 
+0

まあそれはそう。また、無限ループがあり、ファイルに書き込むと悪くなります... – MooingRawr

+0

http://www.pitt.edu/~naraehan/python2/reading_writing_methods.html –

+0

結果は外部ファイルに書き込む必要がありますか?あなたは結果を持っていません –

答えて

0

あなたの出力を書き込むために別のファイルを開く必要があります。

from random import shuffle 


def transform(word): 
    l = list(word) 
    shuffle(l) 
    new_word = ''.join(l) 
    return new_word[:3].replace('o', '0') 


input_file = 'randomwords.txt' 
output_file = 'passwords.txt' 
with open(input_file, 'r') as word_file: 
    with open(output_file, 'wb') as pwd_file: 
     for r in word_file: 
      word = r.strip() 
      password = transform(word) 
      pwd_file.write(password + '\n') 

参考:ファイルを開くため、書き込みモードでファイルを開いて、代わりに印刷すると、そのファイルに書き込む方法を知っているようhttp://www.pythonforbeginners.com/files/reading-and-writing-files-in-python

+0

この種の作品は、私がそれを見るときに出力ファイルは空です。 – Quarismo

+0

@Quarismo興味深い。変換関数を共有できますか?私はテスト用にrandom \ ncolor \ nをrandomwords.txtに入れて動作させています。 – sangheestyle

+0

もちろん、質問に追加しました。 – Quarismo

関連する問題