简体   繁体   中英

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:

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

>>> 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

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.

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:

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

>>> 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

Am I doing this one right or I am wrong(IF yes then please Help ME)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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