Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.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
Mysql Python2.7试图使用用户名和密码迭代文件,无法正确获取数据_Mysql_Python 2.7 - Fatal编程技术网

Mysql Python2.7试图使用用户名和密码迭代文件,无法正确获取数据

Mysql Python2.7试图使用用户名和密码迭代文件,无法正确获取数据,mysql,python-2.7,Mysql,Python 2.7,我使用的是Python 2.7,我有一个文本文件,格式如下: Username: JohnDoe Password: JohnsPass ------------------------ Username: Bob Password: BobsPass ------------------------ 它的开始和你上面看到的一样,结束和你看到的一样 我尝试了以下方法来获取字典/列表中的数据,以便将其导入mysql thefile = open("theaccounts.txt","r") m

我使用的是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']
Username: JohnDoe

Password: JohnsPass

Username: Bob

Password: BobsPass
我一直在尝试获取用户名/密码,如下所示:

for userinfo in myAcctList:
    if userinfo.startswith('-------------'):
        pass
    else:
        print userinfo
它告诉我:

['Username: JohnDoe\n',
 'Password: JohnsPass\n',
 '------------------------\n',
 'Username: Bob\n',
 'Password: BobsPass\n',
 '------------------------\n']
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\u用户
字典,所以发生了翻倍。因此,当您读取第二个用户名时,您只是覆盖了该词典的用户名,而不是开始新词典。这就是为什么我的答案在将当前用户添加到
users
列表后重新分配
cur_user={}


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

使用
作为分隔符拆分行。然后将第一部分用作字典中的键,第二部分用作值。当你到达
----
行时,将字典添加到你的列表中。我理解你的意思,只是不知道如何去做。我从你的例子中尝试了几件事情,但都没有成功。我基本上得到了和以前一样的加倍,然后我尝试直接使用你展示的内容,但没有产生预期的结果。我更新了我的答案,解释了你在第一次更新中看到的内容。我想我现在理解得更好了。唯一的问题是,现在它所做的就是这样打印:{'Username':'JohnDoe'}#我如何跳过这个?{'Username':'JohnDoe','Password':'johnpass'}我试着输入一个“if not cur_user['Password']:pass”让它跳过这个,但它失败了,特别是它没有说出“keyrerror:‘Password’”,我知道它不在那里,因为我试图传递它并获取我需要的信息。在我的第一次更新中,我将把cur_用户放在哪里?(如果你能指出这一点,请尽可能多地学习)。好的,我知道应该把它放在哪里,但是,我得到的是相同的东西,显示用户名,然后是用户名/密码。如何让它跳过用户名,只显示用户名/密码?还有没有更好的方法来存储这些信息?像pickle或csv文件?好吧,我想出来了!我添加了一个“if len(cur_user)==2:”我完全明白我在寻找什么。非常感谢你帮了我的忙!如果您有任何阅读建议,请告知。我会将此标记为已完成,再次感谢!!