所以我目前有这段代码来读取一个类似这样的accounts.txt 文件:
username1:password1
username2:password2
username3:password3
然后我(感谢这里的一位成员)读取accounts.txt 文件并在用户名和密码处将其拆分,以便稍后可以打印它。当我尝试使用以下代码单独打印第 1 行的用户名和密码时:
with open('accounts.txt') as f:
credentials = [x.strip().split(':') for x in f.readlines()]
for username,password in credentials:
print username[0]
print password[0]
它打印出以下内容:(
j
t
a
2
a
3
这些是我在文本文件中的三行,正确分割,但是它打印所有行,仅打印第一个字母每行。)
我尝试了几种不同的方法,但没有成功。有人知道该怎么做吗?
感谢您的帮助。真的很感激。这是我第二天的节目,对于这样一个简单的问题我深表歉意。
So I currently have this code to read an accounts.txt file that looks like this:
username1:password1
username2:password2
username3:password3
I then have this (thanks to a member here) read the accounts.txt file and split it at the username and password so I can later print it. When I try to print line 1 with the username and password separate with this code:
with open('accounts.txt') as f:
credentials = [x.strip().split(':') for x in f.readlines()]
for username,password in credentials:
print username[0]
print password[0]
It prints out this:
j
t
a
2
a
3
(These are the three lines I have in the text file, properly split, however it's printing all the lines and only the first letter of each line.)
I've tried a few different methods with no luck. Anyone have an idea on what to do?
Thank you for all your help. It's really appreciated. This is my second day programming and I apologize for such a simple question.
发布评论
评论(2)
用户名
和密码
是字符串。当您对字符串执行此操作时,您将获得字符串中的第一个字符:不要这样做。只需
打印用户名
。一些进一步的解释。
credentials
是字符串列表的列表。打印出来时看起来像这样:要获取一对用户名/密码,您可以执行以下操作:
打印凭证[0]
。结果将是这样的:或者,如果您执行了
打印凭据[1]
,则为:您还可以执行一项称为“拆包”的操作,这就是 for 循环的作用。您也可以在 for 循环之外执行此操作:
结果将是
再一次,如果您采用像
'username1'
这样的字符串并采用其中的单个元素,如下所示:您会得到一个字母,<代码>u。
username
andpassword
are strings. When you do this to a string, you get the first character in the string:Don't do that. Just
print username
.Some further explanation.
credentials
is a list of lists of strings. It looks like this when you print it out:To get one username/password pair, you could do this:
print credentials[0]
. The result would be this:Or if you did
print credentials[1]
, this:You can also do a thing called "unpacking," which is what your for loop does. You can do it outside a for loop too:
The result would be
And again, if you take a string like
'username1'
and take a single element of it like so:You get a single letter,
u
.首先,我想说,如果这是您第二天的编程,那么使用
with
语句和 列表已经理解了!正如其他人已经指出的那样,由于您使用
[]
索引与包含str
的变量,因此它将str
视为如果它是一个数组,那么您将获得指定索引处的字符。我想我应该指出几件事:
1)您不需要使用
f.readline()
来迭代文件,因为文件对象f
是一个可迭代对象(它定义了 __iter__ 方法,您可以使用getattr(f, '__iter__')
检查该方法。因此您可以这样做:2)您还提到你“好奇如果有没有办法只打印文件的第一行?或者在这种情况下选择第二行、第三行等?”
islice(iterable[, start], stop[, step])<
itertools
包中的 /code> 函数作品非常适合,例如,只获得第二个和第二个。第三行(记住索引从 0 开始!!!):或者获取每隔行:
花时间学习 itertools (及其 食谱!!!);它会简化你的代码。
First, I'd like to say if this is your second day programming, then you're off to a good start by using the
with
statement and list comprehensions already!As the other people already pointed out, since you are using
[]
indexing with a variable that contains astr
ing, it treats thestr
as if it were an array, so you get the character at the index you specify.I thought I'd point out a couple of things:
1) you don't need to use
f.readline()
to iterate over the file since the file objectf
is an iterable object (it has the__iter__
method defined which you can check withgetattr(f, '__iter__')
. So you can do this:2) You also mentioned you were "curious if there's a way to print only the first line of the file? Or in that case the second, third, etc. by choice?"
The
islice(iterable[, start], stop[, step])
function from theitertools
package works great for that, for example, to get just the 2nd & 3rd lines (remember indexes start at 0!!!):Or to get every other line:
Spend time learning itertools (and its recipes!!!); it will simplify your code.