473,385 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 4059
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Lad | last post by:
What is the best( easiest)way how to sort a file? I have a file where each record consists of 3 fields( 3 words) and I would like to sort records by the first field( word)in each record. Any idea?...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
3
by: micklee74 | last post by:
hi I have a file with columns delimited by '~' like this: 1SOME STRING ~ABC~12311232432D~20060401~00000000 2SOME STRING ~DEF~13534534543C~20060401~00000000 3SOME STRING ...
4
by: jonathan184 | last post by:
Hi I have a perl script, basically what it is suppose to do is check a folder with files. Now the files are checked using a timestamp with the command ls -l so the timestamp in this format is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.