473,473 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2540
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...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.