简体   繁体   English

2 如何将文件中的行与字符串进行比较

[英]How 2 compare lines from a file with a string

I recently began to develop a project for my GitHub page as I am learning python programing language so I am trying to Create a Student DBMS program in which there are multiple functions as One of them is explained below:我最近开始为我的 GitHub 页面开发一个项目,因为我正在学习 python 编程语言,所以我试图创建一个学生 DBMS 程序,其中有多个功能,其中一个功能如下所述:

In this Function, I am trying to read data from the file in which only usernames and password are stored and after that, I am getting the user input (USERNAME AND PASSWORD) and storing them in the form of string using this method --> string = 'user = ' + user + ',' + 'pass = ' + password (as the USERNAMES AND PASSWORDS stored in the file looks like this --> user = %username%, pass = %password% ) after this when I am trying to compare both the string it doesn't show any output. Full Working Steps are Given Below在这个 Function 中,我试图从只存储用户名和密码的文件中读取数据,之后,我获取用户输入(用户名和密码)并使用此方法以字符串形式存储它们 --> string = 'user = ' + user + ',' + 'pass = ' + password (因为存储在文件中的用户名和密码看起来像这样 --> user = %username%, pass = %password% )在此之后 when我正在尝试比较它不显示任何 output 的字符串。下面给出了完整的工作步骤

>>> user = 'admin'
>>> password = 'password'
>>> string = 'user = ' + user + ',' + 'pass = ' + password
>>> print(string)
user = admin, pass = password
>>> type(string)
<class 'str'>
>>> import os
>>> os.chdir('C:\\Users\\alexm\\Desktop\\Python Practice')
>>> file = open('aman.txt','r')
>>> for line in file:
        print(line)


user = admin, pass = password

user = adminn, pass = pass
>>> for line in file:
        type(line)
        print(line)


>>> for line in file:
        type(line)
        print(line)

NOTE: THE ABOVE LINES ARE DIRECTLY TAKEN FROM THE PYTHON SHELL注意:以上几行直接取自 PYTHON SHELL

Am I doing this one right or I am wrong(IF yes then please Help ME)我这样做是对还是错(如果是,请帮助我)

Have you tried doing:您是否尝试过:

If line == string:
    print('they are the same!')
else:
    print(f'{string} is not the same as {line}') 

By the way, I would recommend change the variable name string to user_input or something like that.顺便说一句,我建议将变量名称string更改为user_input或类似的名称。

I recently began to develop a project for my GitHub page as I am learning python programing language so I am trying to Create a Student DBMS program in which there are multiple functions as One of them is explained below:我最近开始为我的 GitHub 页面开发一个项目,因为我正在学习 python 编程语言,所以我正在尝试创建一个学生 DBMS 程序,其中有多个功能,其中之一解释如下:

In this Function, I am trying to read data from the file in which only usernames and password are stored and after that, I am getting the user input (USERNAME AND PASSWORD) and storing them in the form of string using this method --> string = 'user = ' + user + ',' + 'pass = ' + password (as the USERNAMES AND PASSWORDS stored in the file looks like this --> user = %username%, pass = %password% ) after this when I am trying to compare both the string it doesn't show any output.在此函数中,我试图从仅存储用户名和密码的文件中读取数据,然后,我获取用户输入(用户名和密码)并使用此方法以字符串形式存储它们 --> string = 'user = ' + user + ',' + 'pass = ' + password (因为文件中存储的用户名和密码看起来像这样 --> user = %username%, pass = %password% )我正在尝试比较它没有显示任何输出的字符串。 Full Working Steps are Given Below下面给出了完整的工作步骤

>>> user = 'admin'
>>> password = 'password'
>>> string = 'user = ' + user + ',' + 'pass = ' + password
>>> print(string)
user = admin, pass = password
>>> type(string)
<class 'str'>
>>> import os
>>> os.chdir('C:\\Users\\alexm\\Desktop\\Python Practice')
>>> file = open('aman.txt','r')
>>> for line in file:
        print(line)


user = admin, pass = password

user = adminn, pass = pass
>>> for line in file:
        type(line)
        print(line)


>>> for line in file:
        type(line)
        print(line)

NOTE: THE ABOVE LINES ARE DIRECTLY TAKEN FROM THE PYTHON SHELL注意:以上几行直接取自 Python Shell

Am I doing this one right or I am wrong(IF yes then please Help ME)我做对了还是错了(如果是,请帮助我)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM