- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用的是 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/
我想做的是从 table1 中获取 usernames 和 picURLs WHERE usernames = all the usernames from this query (从表 2 中选择用
我使用字符串“username”作为表的主键, 但是,当保存并获取具有用户名ID的列时,我希望忽略这种情况,以使新用户无法尝试模拟其他用户。 ,例如注册新用户时 用户名=达克森 用户名= DaXoN
在 ReportManager 中,我有很多用户提示这个错误。当我筛选 RS 日志时,我多次看到不同用户的以下错误: session!ReportServer_0-2!1e40!04/20/2011-
我是 Node js新手,我正在尝试将MySql与 Node 连接的示例。当我运行代码时,我得到 Error while performing Query.Error: ER_ACCESS_DENIE
收集整理了15个常用的javaScript正则表达式,其中包括用户名、密码强度、整数、数字、电子邮件地址(Email)、手机号码、身份证号、URL地址、 IPv4地址、 十六进制颜色、 日期、 QQ
我正在做一个需要以下内容的项目。 服务器端的 WCF 服务 (.NET 3.5) 客户端的 WPF 客户端 (.NET 3.0) 我有一个现有的应用程序,我必须使用(在服务器端)的身份验证和授权。我还
用户名,密码和电子邮件的标准最小和最大长度是多少? 最佳答案 我对此问题有疑问,因为它预先假设应该有一个标准,并且所有服务的值都应该相同。 我认为为这些字段中的任何一个(例如密码字段)加上最大长度是没
我看到in the standard我可以拥有 autocomplete要么等于 username或 email在输入字段上。 就我而言,用户名实际上是用户的电子邮件。 什么是最好的autocompl
我正在尝试编写一些网站代码并创建用户名和密码。我不知道这是否完全合乎逻辑,或者即使代码已修复,它是否仍能工作,但我是一个初学者,我正在从零开始工作。这是我所拥有的: alert("Welcome.
我想使用ncat编写一个简单的bash脚本,以打开与ISP及其端口的连接。 第一个命令是: nc address port 完成此操作后,首先提示我提供一个用户名。我必须按ENTER键,然后提示我提供
我收到此错误是因为我创建了一个自定义用户模型,并且在完成所有设置后(将其设置为django / contrib / auth / models.py),我将电子邮件设置为USERNAME_FIELD,
我有 3 台 Windows 7 Professional 机器。两台被设置为安装了 Git 的开发机器(一台台式机和一台笔记本电脑)。第三个是设置为安装了 Git 的文件服务器,并且它还作为 ssh
def login(): UserName = input("Please enter your username: ") passw = input("Please enter yo
我正在 Kivy 中创建一个应用程序。我正在创建一个登录页面,允许用户输入密码和用户名,以验证他们以前是否使用过我的应用程序。我将用户名和密码存储在名为“users”的 MySQL 数据库中,该表称为
什么是 Mysql 查询 REGEXP 来调用它? @text @user_name @4ll_r1ght @last2 @_last1 @and1more_ 最佳答案 SELECT * FROM u
我在寻找用户名和电子邮件垃圾邮件列表。当用户尝试注册时,我想检查他们输入的用户名或电子邮件是否在用户名/电子邮件垃圾邮件列表中,如果是,则将帐户标记为可疑垃圾邮件并手动验证成员资格。 它背后的后端不是
你好, 我被一个小问题困住了, 我需要允许 muplite 用户能够登录 我的 php 代码与一个成员一起工作,我怎样才能让多个用户可以登录, 如果有更好的使用 MySql 的方法请告诉我 $
是否有一组在编译时创建的定义,我可以使用它们来填充 printk 语句,其中包含有关在哪里以及谁最后构建内核驱动程序的信息?我知道有预定义的 C 宏,我希望在那里找到一些在编译开始时动态设置的东西,但
我正在为我正在开发的网站创建登录脚本。目前,我已将用户名和密码硬编码到脚本中。即 $usersAndPasses = array( 'username1' => 'password1',
我想像这样从成员列表页面收集用户名: http://www.marksdailyapple.com/forum/memberslist/ 我想从所有页面中获取每个用户名, 我想在 linux 中用 b
我是一名优秀的程序员,十分优秀!