pythonに行ごとにtxtリストを読み込ませるにはどうすればよいですか? 私は動作していないようです.readlines()を使用しています。TXTファイルを1行ずつ読む - Python
import itertools
import string
def guess_password(real):
inFile = open('test.txt', 'r')
chars = inFile.readlines()
attempts = 0
for password_length in range(1, 9):
for guess in itertools.product(chars, repeat=password_length):
attempts += 1
guess = ''.join(guess)
if guess == real:
return input('password is {}. found in {} guesses.'.format(guess, attempts))
print(guess, attempts)
print(guess_password(input("Enter password")))
test.txtのファイルは次のようになります。
1:password1
2:password2
3:password3
4:password4
現在、プログラムは、リストの最後のパスワードを使用して動作します(password4) 他のパスワードが入力されている場合、それは過去のすべての実行されますパスワードをリストに追加し、 "none"を返します。
だから、私は一度に1行ずつテストするようにpythonに指示しなければならないと思いますか?
PS。 "return input()"はダイアログボックスが自動的に閉じないように入力され、何も入力されません。
http://stackoverflow.com/questions/8009882ので
guess == real
を比較することによって、あなたはrstrip
を使用する改行を削除するにはFalse
ある
'password1\n' == 'password1'
を比較します/大文字の読み方大ファイルの行単位で –パスワードをプレーンテキストで保存するように思われるのではないかと心配しています。 –
@TomdeGeusあなたの声明は間違いなく有効ですが、もし私が推測しなければならないのは、実際の応用ではなく、おそらく運動です。 – Aaron