471,348 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

Sort lines in a text file

say I have a text file:

zz3 uaa4a ss 7 uu
zz 3 zzzz66 ppazz9
aaaaaaaaa00000zz0

I want to sort the text file. I want the key to be the number after
the two "zz". Or I guess a string of two zz then a number....So
that's 3, 9, 0

I'm trying to say that I want to sort lines in a file based on a
regular expression. How could I do that in Python? I'm limited to
Python 2.1, I can't add any 2nd party newer tools.

Thanks
Lee G.

Jul 22 '07 #1
6 3974
On Jul 22, 1:03 pm, leegold <goldt...@worldpost.comwrote:
say I have a text file:

zz3 uaa4a ss 7 uu
zz 3 zzzz66 ppazz9
aaaaaaaaa00000zz0

I want to sort the text file. I want the key to be the number after
the two "zz". Or I guess a string of two zz then a number....So
that's 3, 9, 0

I'm trying to say that I want to sort lines in a file based on a
regular expression. How could I do that in Python? I'm limited to
Python 2.1, I can't add any 2nd party newer tools.
To save anybody who's tempted to write the whole shebang for you,
please specify which part(s) of the exercise you are having problems
with:
(a) reading lines from a file
(b) extracting a sort key from a line [presuming "number" means
"positive integer"; what do you want to do if multiple lines have the
same number? what if no number at all"]
(c) creating a list of tuples, where each tuple is (key,
line_contents)
(d) sorting the list
(e) creating the output file from the sorted list.
Jul 22 '07 #2
On Sun, 22 Jul 2007 06:03:17 +0300, leegold <go******@worldpost.comwrote:
say I have a text file:

zz3 uaa4a ss 7 uu
zz 3 zzzz66 ppazz9
aaaaaaaaa00000zz0

I want to sort the text file. I want the key to be the number after
the two "zz". Or I guess a string of two zz then a number....So
that's 3, 9, 0

I'm trying to say that I want to sort lines in a file based on a
regular expression. How could I do that in Python? I'm limited to
Python 2.1, I can't add any 2nd party newer tools.
Do your own homework.
Jul 22 '07 #3
On Sun, 22 Jul 2007 06:03:17 +0300, leegold <go******@worldpost.comwrote:
say I have a text file:

zz3 uaa4a ss 7 uu
zz 3 zzzz66 ppazz9
aaaaaaaaa00000zz0

I want to sort the text file. I want the key to be the number after
the two "zz". Or I guess a string of two zz then a number....So
that's 3, 9, 0

I'm trying to say that I want to sort lines in a file based on a
regular expression. How could I do that in Python? I'm limited to
Python 2.1, I can't add any 2nd party newer tools.

Thanks
Lee G.
Shouldn't it be 3, 6, 9, 0
Jul 22 '07 #4
....snip...
To save anybody who's tempted to write the whole shebang for you,
please specify which part(s) of the exercise you are having problems
with:
(a) reading lines from a file
(b) extracting a sort key from a line [presuming "number" means
"positive integer"; what do you want to do if multiple lines have the
same number? what if no number at all"]
(c) creating a list of tuples, where each tuple is (key,
line_contents)
(d) sorting the list
(e) creating the output file from the sorted list.
Thanks, you've done more than enough right here!

(a) through (e) gives me a good start.

Again, thank you for this.

Jul 22 '07 #5
....snip...
>
Do your own homework.
Hush troll.

Jul 22 '07 #6
Daniel wrote:
On Sun, 22 Jul 2007 06:03:17 +0300, leegold <go******@worldpost.comwrote:
>say I have a text file:

zz3 uaa4a ss 7 uu
zz 3 zzzz66 ppazz9
aaaaaaaaa00000zz0

I want to sort the text file. I want the key to be the number after
the two "zz". Or I guess a string of two zz then a number....So
that's 3, 9, 0

I'm trying to say that I want to sort lines in a file based on a
regular expression. How could I do that in Python? I'm limited to
Python 2.1, I can't add any 2nd party newer tools.

Thanks
Lee G.

Shouldn't it be 3, 6, 9, 0
I'm guessing that the 6 comes after 4 z's and not 2 z's in line 2 so it
is ignored.
Jul 23 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.