Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从文件[python]中选择整个字符串_Python_Python 3.x_File - Fatal编程技术网

从文件[python]中选择整个字符串

从文件[python]中选择整个字符串,python,python-3.x,file,Python,Python 3.x,File,我这里的问题是,虽然程序没有直接的语法错误,但如果我只输入部分密码(例如password=password,但我的输入可能只是“pass”) 因此,我在这里要问的是如何在文件中选择整个字符串 是我使用的split,使程序允许正确密码的部分拷贝通过 或者是什么 with open("users.txt") as f: for i, l in enumerate(f): l.split(":") l.strip() if username in

我这里的问题是,虽然程序没有直接的语法错误,但如果我只输入部分密码(例如password=password,但我的输入可能只是“pass”)

因此,我在这里要问的是如何在文件中选择整个字符串

是我使用的
split
,使程序允许正确密码的部分拷贝通过

或者是什么

with open("users.txt") as f:
    for i, l in enumerate(f):
        l.split(":")
        l.strip()
        if username in l:
            print("you exist...") 
            password= input("please input your password ")
            while password not in l:
                password=input("Enter your password")   
该文件如下所示:

me:password123 
user1:12345678 
user2:12345678
username = input("Username: ")

with open("users.txt") as f:
    for l in f:
        l = l.strip() # save the returned value
        uname, upass = l.split(":") # split at : and assign it to variables           
        if username == uname: # check if the usernames match
            print("you exist...") 
            password= input("please input your password ")

            while password != upass: # check to see if the password match
                password=input("Enter your password") 

            break # break out of the for loop

如果左列是用户名,右列是密码

string.strip
string.split
这两个操作都不是就地操作,则需要像下面这样捕获返回的参数:

me:password123 
user1:12345678 
user2:12345678
username = str(input("Username: "))
password = str(input("Password: "))

f = open("test.txt", "r").read().splitlines()
for index in range(len(f) - 1):
    if f[index] == username and f[index + 1] == password:
        print("Login made")
username = input("Username: ")

with open("users.txt") as f:
    for l in f:
        l = l.strip() # save the returned value
        uname, upass = l.split(":") # split at : and assign it to variables           
        if username == uname: # check if the usernames match
            print("you exist...") 
            password= input("please input your password ")

            while password != upass: # check to see if the password match
                password=input("Enter your password") 

            break # break out of the for loop

仍然有许多改进可以做,我将把这些留给你去做(这是有意的)。尽管也有一些错误需要修复,例如:如果用户名或密码中有冒号“:”它也会被拆分。

我会使用字典存储密码数据,用户名作为密钥,密码作为值。当然,在真正的程序中,您应该而不是将密码保存为纯文本

import sys

passwords = {}

with open("users.txt") as f:
    for line in f:
        name, word = line.split(":")
        passwords[name.strip()] = word.strip()

username = input("Please enter your username: ")
if username in passwords:
    print("you exist...")
    real_password = passwords[username]
    password = ''
    while password != real_password:
        password = input("Please enter your password: ")
else:
    print("you don't exist")
    sys.exit()

print('Welcome,', username)
我们知道“users.txt”的每一行上正好有两个项目,因此我们可以使用
line.split(“:”)
来构建包含这两个字符串的列表。我们可以使用元组分配将这些字符串分配给单独的变量:

name, word = line.split(":")

但是,这些字符串的两侧可能都有不需要的空格,因此在将它们放入
密码
字典之前,我们需要对每个字符串调用
.strip()
方法。

您的文件是什么样子的?(编辑您的问题以将其包括在内)我:password123 user1:12345678 user2:12345678为什么要丢弃由
l.split(“:”
)返回的列表?
string.split
string.strip
不是就地操作。
l.split(“:”
将字符串
l
拆分为子字符串,将子字符串放入列表,它返回。但是您没有保存返回值。类似地,
l.strip()
创建了一个新字符串,其中包含
l
中的字符,前导和尾随空格被删除。但您没有保存新字符串。在Python中,字符串方法不会修改原始字符串,因为Python字符串是不可变的。虽然这个代码片段可以解决这个问题,但它确实有助于提高文章的质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。