[Tutor] alphabetizing a file by lines

Lloyd Kvam pythonTutor at venix.com
Sun Jul 18 01:13:56 CEST 2004


You've gotten two suggestions for how to do this using Python's sorting
abilities.  I'm going to assume you are really just trying to do an
exercise where you code more of the details.

I think it would help if you started with a clearer statement of how you
are accomplishing your task.  After looking through the code I think
this describes what you are trying to do:

For each letter of the alphabet:
	For each line of the input file:
		if the line starts with this letter:
			write the line to the output file

It should not be too hard to flesh those statements out into Python
code.  I think this kind of approach will help with organizing your code
and untangling your loop logic.  If these statements do not fit your
intent, write your own statements and then write the program around
them.


On Sat, 2004-07-17 at 16:22, Dragonfirebane at aol.com wrote:
> Hello all,
>  
> I'm trying to write a program that alphabetizes a file by the first
> letter on each line. I'm having some problems because as soon as the
> program finds a letter, it looks for the next letter, ignoring
> subsequent appearances of that letter. I can think of a couple ways to
> fix this but none of them seem to work. The first of these would be to
> add a special character to lines that have already been alphabetized,
> but file.write() writes to the end of a file and i would need to write
> the character at the current position in the file. This might be
> circumvented by creating a file for each line that is alphabetized,
> but that seems a bit extreme . . . The code is below. Any suggestions
> would be appreciated. Future concerns will be alphabetizing past the
> first letter.
>  
> ##############
> def linum():
>     global i
>     linu = open("%s%s" % (fn, ext), "r")
>     i = 0
>     for line in linu.readlines():
>         i += 1
>     linu.close()
> def alph():
>     alp = open("alphebatized%s%s" % (fn, ext), "w")
>     pal = open("prealp%s%s" % (fn, ext), "w")        ## eventually for
> writing "\xfe" after a line that has been alphabetized
>     read = open("%s%s" % (fn, ext), "r")                ## same reason
> for this line until read.close()
>     for line in read.read():
>         pal.write(line)
>     pal.close()
>     read.close()
>     print i
>     for do in range(i):
>         falp(alp)
>     alp.close()
> def falp(alp):
>     global a
>     read = open("prealp%s%s" % (fn, ext), "r")
>     for line in read.readlines():
>         try:
>             alpn = re.compile("%s(?!\xfe)" % alpha[a], re.IGNORECASE)
>             falpn = alpn.match(line)
>             if falpn:
>                 print line
>                 alp.write(line)
>                 a += 1
>                 break
>         except IndexError:
>             pass
> import re
> import string
> i = 0
> a = 0
> alpha = ' '.join(string.ascii_letters[:26]).split()
> fn = raw_input("Please enter name of file you wish to prioritize: ")
> ext = raw_input("Please enter extension of file you wish to
> prioritize: ")
> linum()
> alph()
> ################
>  
> Here is random.txt, the file being alphabetized:
>  
> K
> cx
> c
> cd
> e
> X
> y
> v
> l
> f
> w
> Q
> z
> h
> r
> i
> T
> s
> p
> d
> m
> n
> a
> o
> j
> u
> b
> G
> 
>  
> Thanks in advance, 
> Orri
>  
> Email: dragonfirebane at aol.com
> AIM: singingxduck
> Programming Python for the fun of it. 
> 
> ______________________________________________________________________
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 

Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-653-8139
fax:	801-459-9582



More information about the Tutor mailing list