gpt4 book ai didi

mysql - Python 2.7 尝试使用用户名和密码迭代文件,无法正确获取数据

转载 作者:行者123 更新时间:2023-11-29 00:03:28 25 4
gpt4 key购买 nike

我使用的是 Python 2.7,我有一个完全按照这种格式开头的文本文件:

Username: JohnDoe
Password: JohnsPass
------------------------
Username: Bob
Password: BobsPass
------------------------

它开始的方式与您在上面看到的相同,结束的方式与您看到的相同。

我尝试了以下方法来获取字典/列表中的数据,以便将其导入 mysql。

thefile = open("theaccounts.txt","r")

myAcctList=[]
for line in thefile:
myAcctList.append(line)

thefile.close()

这告诉我:

['Username: JohnDoe\n',
'Password: JohnsPass\n',
'------------------------\n',
'Username: Bob\n',
'Password: BobsPass\n',
'------------------------\n']

我一直在尝试这样获取用户名/密码:

for userinfo in myAcctList:
if userinfo.startswith('-------------'):
pass
else:
print userinfo

它告诉我:

Username: JohnDoe

Password: JohnsPass

Username: Bob

Password: BobsPass

我怎样才能把它放在一行或一本字典中,这样我就可以将它们导入到 mysql 数据库中?我已经尝试了各种方法,但是,它们要么都出错了,要么打印结果加倍,显示用户名两次,密码是以前用户名的密码。

有什么办法可以做到:

print "Username is: %s and password is: %s" % (usernameis,passwordis)

因为我想设置变量一次性放入mysql记录,而不是匹配用户名然后插入密码。

请提供建议或解决方案,我一直在努力解决这个问题,但确实没能成功。感谢所有输入,非常感谢!

更新:

我修改了你给我看的内容并想出了这个:

cur_user={}
countlogins=0
for userinfo in myAcctList:
if userinfo.startswith('------------------------'):
pass
else:
forusername=userinfo.split(':')
print "I see index 0 as: %s" % forusername[0]
print "I see index 1 as: %s" % forusername[1]
cur_user[forusername[0]] = forusername[1].strip()
print cur_user
time.sleep(3) #just so I could see the top of the list

这更接近了,但是,它仍然做一些奇怪的事情,两次显示用户名并显示前一行的密码,然后显示它应该是的密码。它也只在它到达第一行时显示用户名(我猜是因为它还没有迭代到第二行)。

打印出来是这样的:

I see index 0 as: Username
I see index 1 as: JohnDoe

{'Username': 'JohnDoe'}
I see index 0 as: Password
I see index 1 as: JohnPass

{'Username': 'JohnDoe', 'Password': 'JohnPass'}
I see index 0 as: Username
I see index 1 as: BobTheUser

{'Username': 'BobTheUser', 'Password': 'JohnPass'}
I see index 0 as: Password
I see index 1 as: BobsPass

{'Username': 'BobTheUser', 'Password': 'BobsPass'}
I see index 0 as: Username
I see index 1 as: ThisOtherUser

{'Username': 'ThisOtherUser', 'Password': 'BobsPass'}
I counted 5 logins
I see index 0 as: Password
I see index 1 as: ThisOtherUserPass

{'Username': 'ThisOtherUser', 'Password': 'ThisOtherUserPass'}
I see index 0 as: Username
I see index 1 as: YetOneMore

我不明白为什么它会像那样加倍,或者为什么它需要第二轮才能获得正确的信息。这将阻止(如果我没记错的话)正确插入 mysql 数据库。我想让它告诉我一次我需要知道的信息,这样我就可以知道它会插入正确的信息。

感谢您的宝贵时间和协助!

第二次更新:

我也试过:

theFile = open("theaccounts.txt","r")
users = []
cur_user = {}
for line in theFile:
if line.startswith('------'):
users.append(cur_user)
cur_user = {}
else:
fields = ':'.split(line)
cur_user[fields[0]] = fields[1].strip()
theFile.close()

这给我一个错误:

---> 10         cur_user[fields[0]] = fields[1].strip()
11 theFile.close()

IndexError: list index out of range

然后我尝试了:

theFile = open("theaccounts.txt","r")
users = []
cur_user = {}
for line in theFile:
if line.startswith('------'):
users.append(cur_user)
cur_user = {}
else:
fields = ':'.split(line)
try:
cur_user[fields[0]] = fields[1].strip()
print cur_user
except:
pass
theFile.close()

这只给了我:

[{},
{},
{},
{},
{},
{},
{},
{}]

请帮我解决这个问题,我真的不明白为什么这么难。

第三次更新:

好的,明白了!对于可能需要这样做或遇到麻烦的其他人,这是最终结果:

countlogins=0
theFile = open("theaccounts.txt","r")
myAcctList=[]
cur_user={}
for line in theFile:
if line.startswith('------'):
countlogins=countlogins+1
print cur_user
myAcctList.append(line)
cur_user={}
else:
forusername=line.split(':')
cur_user[forusername[0]] = forusername[1].strip()
theFile.close()
print "I counted %s logins" % countlogins

我在最后做了额外的计数以验证它与我被告知的相符。

感谢您的帮助!

最佳答案

使用 : 作为分隔符拆分行。然后将第一部分用作字典中的键,将第二部分用作值。当您到达 ---- 行时,将字典添加到您的列表中。

users = []
cur_user = {}
for line in theFile:
if line.startswith('------'):
users.append(cur_user)
cur_user = {}
else:
fields = line.split(':')
cur_user[fields[0]] = fields[1].strip()

在您的第一次更新中,由于您对所有内容都使用相同的 cur_user 字典,因此发生了加倍。所以当你读取第二个用户名时,你只是覆盖了那个字典的用户名,而不是开始一个新的用户名。这就是为什么我的答案在将当前用户添加到 users 列表后重新分配 cur_user = {}

将用户名和密码同时放入字典需要两个步骤,因为当您读取文件的第一行时,您还没有读取第一个密码。由于您在每一行之后打印字典,因此您会在第一行之后看到这个部分结果。在到达 ----- 分隔线之前,您不应该尝试向数据库中添加内容,这就是您知道拥有两个字段的方式。

关于mysql - Python 2.7 尝试使用用户名和密码迭代文件,无法正确获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28619878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com