[Tutor] Uniform 'for' behavior for strings and files
wescpy at gmail.com
Thu Jun 12 08:33:10 CEST 2008
> for item in block:
> where block is a file, then item becomes each line of the file in turn
> But if block in a large string (let's say the actual text that was in
> the file), item becomes each character at a time. Is there a way to
> have a uniform iteration irrespective of whether block is a file or
> string (ideally one \n-delimited line at a time)?
i would definitely take Kent's and Alan's suggestions of using the
str.splitlines() method whenever possible... (i think alan misspelled
it as "split" but we know what he meant! ;-) their idea is to use
isinstance() to tell the different between a file and a string, and if
it's a file, it iterate over each line as usual, but for a large
string, to break it up into a group of lines using str.splitlines().
another approach is to "turn that string into a file"-like object by
using the StringIO.StringIO class. what that does is that it creates a
file-like interface to a large string. once you have that object, then
you can iterate over it like a normal file, even tho it's a string:
large = '''Line 1
stringFile = StringIO.StringIO(large)
for eachLine in stringFile:
# process each line here. ie.
print eachLine, # trailing ',' suppresses print's \n
stringFile.close() # free the object
there is also a faster version of this class written in C:
cStringIO.StringIO. here are the module docs:
the advantage of doing it this way is that your code that iterates
over a file can stay unchanged... you just create the StringIO object
inside the if isinstance() clause.
hope this helps!
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
More information about the Tutor