2017-12-28 5 views
0

나는 계정을 등록하는 프로그램을 가지고 있으며, 이제는 계정을 기록하는 기능을 사용하고 있습니다. 텍스트 파일을 분리하기 위해 "사용자 이름"과 "암호"목록을 저장했습니다. .파이썬을 사용하여 텍스트 파일의 목록 검색

usernames = [] 
passwords = [] 
usernames.append(username) 
passwords.append(password) 

usernamesFile = open("usernames.txt","a") 
for usernames in usernames: 
    usernamesFile.write("%s\n" % usernames) 
usernamesFile.close() 

passwordsFile = open("passwords.txt", "a") 
for passwords in passwords: 
    passwordsFile.write("%s\n" % passwords) 
passwordsFile.close() 

while True: 
    loginUsername = raw_input("Enter your username: ") 
    usernamesFile = open("usernames.txt", "r") 
    lines = usernamesFile.readlines() 
    if loginUsername in usernames: 
     index = usernames.index(username) 
     break 
    else: 
     print "Username not found" 
     continue 

내 문제는 내가 텍스트 파일을 테스트 한 내가 등록한 사용자 이름을 모두 저장에도 불구하고, 검색은 여전히 ​​돌아 오면 "사용자 이름을 찾을 수 없습니다." 나는 파이썬 전문가가 아니므로 간단한 방법으로 설명하면 좋을 것입니다.

+1

그것은 당신의 코드처럼 보이지 않는 지금 내가 추측하고있어'lines', 아무것도 당신을 통해 검색하고 싶었 무엇입니까? (또한 : 참고로 : 파이썬을 배우면서도 암호 목록을 지우는 아이디어를 즉시 제거해야합니다.) –

+0

암호 목록을 일반 텍스트로 저장하지 않으려면 어떻게합니까? – ghj

답변

0

당신이 "\n"를 제거하거나 당신이 그것을 추가하지 않는

if loginUsername in usernames: 

항상 False 당신의 상태 때문에 각 이름의 끝에 "\n"을 추가하는이 방법

usernamesFile.write("%s\n" % usernames) 

를 작성하기 때문이다 loginUsername

1

lines은 (는)에서 읽었습니다.코드를 사용하여 사용자 이름을 검색 할 수 있습니다. 그래서, 같은 코드를 수정 :

usernames = [] 
passwords = [] 
usernames.append(username) 
passwords.append(password) 

usernamesFile = open("usernames.txt","a") 
for usernames in usernames: 
    usernamesFile.write("%s\n" % usernames) 
usernamesFile.close() 

passwordsFile = open("passwords.txt", "a") 
for passwords in passwords: 
    passwordsFile.write("%s\n" % passwords) 
passwordsFile.close() 

while True: 
    loginUsername = raw_input("Enter your username: ") 
    usernamesFile = open("usernames.txt", "r") 
    lines = usernamesFile.readlines() 
    if loginUsername in lines: 
     index = lines.index(username) 
     break 
    else: 
     print "Username not found" 
     continue 
관련 문제