472,958 Members | 2,434 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 4033
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.