looping through a file
Andrew Bennetts
andrew-pythonlist at puzzling.org
Wed Apr 30 09:53:13 EDT 2003
On Wed, Apr 30, 2003 at 12:01:30PM +0000, Alex Martelli wrote:
[...]
> I think this will most likely solve your practical problems. However,
> supposing for the sake of argument that InFile could be SO large that
> reading it all at once with readlines would be a problem (say, tens
> of megabytes), you could solve that too, for example, using again
> modern Python (2.2 or better in this case):
>
> InFile = open('users.dat', 'r')
>
> for line in InFile:
> file_username = line.strip()
> file_password = InFile.next().strip()
> if file_username==users_username and file_password==users_password:
> authorized = 1
> break
> else:
> authorized = 0
>
> InFile.close()
I thought there might be something in 2.3's itertools module to help with
this -- I was hoping you'd be able to write something like:
InFile = open('users.dat', 'r')
for username, password in multistep(InFile, 2):
if username == users_username and password == users_password:
authorized = 1
break
else:
authorized = 0
InFile.close()
Where "multistep" would probably have a better name, and be defined like:
def multistep(iterable, count):
# XXX: What should happen when len(iterable) % count != 0 ?
iterator = iter(iterable)
while 1:
values = []
for i in range(count):
values.append(iterator.next())
yield values
I couldn't see anything in itertools to save me writing that out. But I
just figured out how to do it with itertools -- izip can do it:
def multistep(iterable, count):
return itertools.izip(*((iter(iterable),)*count))
I guess you could write this directly:
iterator = iter(InFile)
for username, password in izip(iterator, iterator):
...
So it's probably not worth defining a seperate function...
-Andrew.
More information about the Python-list
mailing list