473,289 Members | 2,141 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,289 software developers and data experts.

How to sort records in file

Lad
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?
Thanks for help
Lad
Jul 18 '05 #1
3 2535
On 2004-08-13, Lad <ex****@hope.cz> wrote:
What is the best( easiest)way how to sort a file?
$ man sort

;)
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.


lines = file('myfilename').readlines()
lines.sort()
for l in lines:
print l,

--
Grant Edwards grante Yow! I just had my entire
at INTESTINAL TRACT coated
visi.com with TEFLON!
Jul 18 '05 #2
Grant Edwards wrote:
On 2004-08-13, Lad <ex****@hope.cz> wrote:
What is the best( easiest)way how to sort a file?


$ man sort

;)
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.


lines = file('myfilename').readlines()
lines.sort()
for l in lines:
print l,


Or

lines = file('myfilename').readlines()
lines = [(line.split(None, 1)[0], i, line)
for (i, line) in enumerate(lines)]
lines.sort()
lines = [line for (_, _, line) in lines]
for l in lines:
print l,

if you want to keep the order stable.

Peter
Jul 18 '05 #3
ex****@hope.cz (Lad) writes:
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.


If you're using Linux, the simplest way is with the "sort" command:

sort infile -o outfile

This will sort files of basically unlimited size, as long as you have
enough disk space. The files don't have to fit in memory.

Since you're posting on c.l.py, maybe you're really asking how to read
the file in sorted order in a Python program:

import os
sorted = os.popen("sort infile")

runs the sort command in an external process.

Finally, there's a built-in sorting operation on lists:

def compare(a, b):
def key(line):
return line.split()[0]
return cmp(key(a), key(b))
lines = open("infile").readlines()
lines.sort(compare)

but that requires reading the whole file into memory, etc.
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Nova's Taylor | last post by:
Hi folks, I am a newbie to Python and am hoping that someone can get me started on a log parser that I am trying to write. The log is an ASCII file that contains a process identifier (PID),...
18
by: googleboy | last post by:
I didn't think this would be as difficult as it now seems to me. I am reading in a csv file that documents a bunch of different info on about 200 books, such as title, author, publisher, isbn,...
1
by: Derek Tinney | last post by:
Hi, I'm having difficulty building an XLST file that allows me to sort a list of log records. I put together an XSL file that allows me to output a copy of the input file and then I attempted...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
1
by: John | last post by:
I was trying to change a BASIC program into an ASP page, but one of the things my basic program does is sort the file that it's going to be looking at before reading it line by line. Is there a...
4
by: Nhmiller | last post by:
This is directly from Access' Help: "About designing a query When you open a query in Design view, or open a form, report, or datasheet and show the Advanced Filter/Sort window (Advanced...
3
by: happy | last post by:
/* Book name : The prodessional programmers guide to C File name : E:\programs\tc\iti01\ch09\main\01setupm.c Program discription: file setuping -up -Version 01-ver01-W Logic ...
3
by: srikanth | last post by:
please give me any helpful logics for the subject
18
by: xahlee | last post by:
Last year, i've posted a tutorial and commentary about Python and Perl's sort function. (http://xahlee.org/perl-python/sort_list.html) In that article, i discussed a technique known among...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.