Python无法从文本文件正确读取? [英] Python not reading from a text file correctly?

查看:52
本文介绍了Python无法从文本文件正确读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,在列表中的文本文件中有一封电子邮件和一个密码,其格式如下: ["tom@gmail.com","Password1"],["harry@gmail.com].com","Password2] .

I have a code where I have an email and a password in a text file in a list, formatted like this: ["tom@gmail.com","Password1"],["harry@gmail.com","Password2].

我的代码从文本文件中读取并将其与用户输入进行比较,以查看文本文件中是否存在该代码.由于某些原因,当我输入第一封电子邮件"tom@gmail.com" 时,它将从文本文件中读取该邮件,但不会从"Password1" 中读取该邮件,但是会读取"harry@gmail.com"以及" Password2"正确.我找不到解决此问题的方法并解决此问题?没有收到错误,所以我不确定它出了问题.这是我的代码:

My code reads from the text file and compares it with a user input to see if it is present in the text file. For some reason when I input the first email "tom@gmail.com" it reads that from the text file but not "Password1", however it reads "harry@gmail.com" correctly as well as "Password2" correctly. I cant find any way to get around this and fix this? No error is received so I'm not sure where its going wrong. Here is my code:

def Login():
    with open("logindata.txt","r") as file:
        for line in file:
            line=line.split(",")
            logindata.append([line[0],line[1].rstrip()])
        print(logindata)
    found=False
    while found == False:
        email=input("Enter email:")
        password=input("Enter password:")
        with open("logindata.txt","r")as file:
            for line in file:
                line = line.split(",")
                if email in line:
                    print("Correct email")
                if password in line:
                    print("Correct Password")
                    found=True
if __name__ == "__main__":
Login()

如果代码已运行并且您尝试了我一开始提供的输入内容,则仅第二个电子邮件和密码组合有效.文本文件如下所示:文本文件格式

if the code is run and you try the inputs I gave at the start then only the second email and password combination works. Here is what the text file looks like text file format

附加:

username=input("Enter a new email")
        password=input("Enter a password")
        with open('logindata.txt','a')as file:
            line='\n'+ username + ',' + password
            file.write(line)
        print("user added")

推荐答案

您正在阅读带有行分隔符的文本 .您的行看起来不是这样的:

You are reading text with line separators. Your lines don't look like this:

tom@gmail.com,Password1

它们实际上是这样的:

tom@gmail.com,Password1\n

在逗号上分割时,您会得到:

When splitting that on the comma, you get this:

line = ['tom@gmail.com', 'Password1\n']

并且测试 'Password1' in line 失败,但是 'Password1\n' 会成功.

and the test 'Password1' in line fails, but 'Password1\n' in line would succeed.

\ n 是换行符.您需要先将其删除;您可以使用 str.strip()从开头和结尾方便地删除 all 空格:

The \n is a newline character. You'd need to remove that first; you could use str.strip() to handily remove all whitespace from the start and end:

for line in file:
    line = line.strip().split(",")
    if email in line:
        print("Correct email")
    if password in line:
        print("Correct Password")

您可以使用 csv ,而不是手动拆分模块读取您的文件:

Rather than manually split, you can use the csv module to read your file:

import csv

with open("logindata.txt", "r") as file:
    reader = csv.reader(file)
    for user, pw in reader:
        if email == user:
            print("Correct email")
        if password == pw:
            print("Correct Password")

这篇关于Python无法从文本文件正确读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆