Iterating over a dictionary's sorted keys
Ivan Van Laningham
ivanlan at home.com
Fri May 26 14:21:54 EDT 2000
Hi All--
Robert Roy wrote:
>
> On Thu, 25 May 2000 22:03:19 -0600, Ivan Van Laningham
> <ivanlan at home.com> wrote:
>
> >Hi All--
> >
> >Alan Daniels wrote:
> >>
> >> On 25 May 2000 08:56:38 GMT, Remco Gerlich
> >> <scarblac-spamtrap at pino.selwerd.nl> wrote:
> >>
> >> [snip...]
> >> >Why the problem with multiple lines and an extra variable here and there?
> >>
> >> Yes, but the short version reminds me very much of one of my favorite
> >> python idioms:
> >>
> >> lines = open("some_file", "rt").readlines()
> >>
> >
> >If you don't want the newlines, you can do this:
> >
> >alist=open("some_file","rt").read().split()
>
> might not work too well with text like:
>
> this is a test
> to see what will happen
> if I do a split on this file
> I think I will get a lot more
> elements
> than I bargained for
>
Again, 1.6 only:
-----------------------------------
#!/c/python/python.exe
import string
import sys
if sys.platform=="win32":
sps='\r\n'
else:
sps='\n'
if __name__=="__main__":
if len(sys.argv)>1:
fn=sys.argv[1]
else:
fn="tsplit.txt"
s=open(fn,"rb").read().split(sps)
print s
-----------------------------------
<more-than-one-way-to-peel-a-grape>-ly y'rs,
Ivan;-)
----------------------------------------------
Ivan Van Laningham
Axent Technologies, Inc.
http://www.pauahtun.org
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
More information about the Python-list
mailing list