Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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 3.x 如何在Python中尝试文件中的行是否与其他文件中的行匹配_Python 3.x_File_Variables_Match_Brute Force - Fatal编程技术网

Python 3.x 如何在Python中尝试文件中的行是否与其他文件中的行匹配

Python 3.x 如何在Python中尝试文件中的行是否与其他文件中的行匹配,python-3.x,file,variables,match,brute-force,Python 3.x,File,Variables,Match,Brute Force,如何“暴力”文件中的每一行,直到找到与之匹配的内容,我的意思是我将save.data和brute.txt中的每一行都转换为两个列表(便于访问),下面是brute.txt: username username1 password password1 这里是save.data(因为这是一个批处理文件游戏,所以不需要引用像“username1”这样的字符串): 因此,我的请求是,我想尝试一下brute.txt中的第1行是否与save.data(即“username1”)中等号之前的内容相匹配,如果不

如何“暴力”文件中的每一行,直到找到与之匹配的内容,我的意思是我将
save.data
brute.txt
中的每一行都转换为两个列表(便于访问),下面是
brute.txt

username
username1
password
password1
这里是
save.data
(因为这是一个批处理文件游戏,所以不需要引用像“username1”这样的字符串):

因此,我的请求是,我想尝试一下
brute.txt
中的第1行是否与
save.data
(即“username1”)中等号之前的内容相匹配,如果不匹配,则传递到下一行,依此类推,直到到达文件末尾(
save.data
)然后,如果
brute.txt
中的第2行与
save.data
中的第1行匹配(匹配),则尝试如果
brute.txt
中的第2行与
save.data
中的第2行中的qual登录前的内容匹配,依此类推。。。最后,当“username”与“username”匹配时,在
save.data
中使用等号后面的值创建一个名为
username
的变量。因此,当“暴力”过程结束时,我必须有两个变量,一个是
username=PlayerName
,另一个是
password=PlayerPass
,以便进一步使用。我尝试了while、for和try循环,但我被卡住了,因为要这样做,我需要知道
save.data
中有什么内容


-如果您不理解某些内容,请发表评论,我会予以澄清。

可能有更有效的方法,但要回答您提出的问题

首先打开
save.data
文件并将内容读入列表:

with open('save.data') as fp:
    save_data = [line.split(' = ') for line in fp.read().splitlines()]
brute.txt
文件执行相同操作:

with open('brute.txt') as fp:
    brute = fp.read().splitlines()
然后只需迭代用户名和密码:

for username, password in save_data:
    if username in brute:
        break
else:
    print("didn't find the username")
for循环中断后,for循环中的
用户名
密码
变量将具有正确的值

(请注意,
else:
在for循环上,而不是if.)

for username, password in save_data:
    if username in brute:
        break
else:
    print("didn't find the username")