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