Thanks lot for your valuable suggestions<br><br><div class="gmail_quote">On Sun, Jun 15, 2008 at 4:04 AM, Dennis Lee Bieber <<a href="mailto:wlfraed@ix.netcom.com">wlfraed@ix.netcom.com</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On Sat, 14 Jun 2008 12:45:47 +0530, "Beema shafreen"<br>
<<a href="mailto:beema.shafreen@gmail.com">beema.shafreen@gmail.com</a>> declaimed the following in<br>
gmane.comp.python.general:<br>
<br>
        Strange: I don't recall seeing this on <a href="http://comp.lang.py" target="_blank">comp.lang.py</a>, just the first<br>
responder; and a search on message ID only found it on gmane...<br>
<div><div></div><div class="Wj3C7c"><br>
> Hi all,<br>
><br>
> I have a file with three columns  i need to sort the file with respect to<br>
> the third column. How do I do it uisng python. I used Linux command to do<br>
> this. Sort but i not able to do it ?<br>
> can any body ssuggest me<br>
<br>
</div></div>Question 1:     Will the file fit completely within the memory of a running<br>
Python program?<br>
<br>
Question 2:     How are the columns defined? Fixed width, known in advance;<br>
tab separated; comma separated.<br>
<br>
If #1 is true, I'd read the file into a list of tuples/sublists (if line<br>
is fixed width columns, read line, manually split on column widths; if<br>
TSV or CSV use the proper options with the CSV module to read the file).<br>
Define a sort key function to extract the key column and use the<br>
built-in list sort method<br>
<br>
        data.sort(key=lambda x : x[2]) #warning, I'm not skilled at lambda<br>
<br>
Actually, if text sort order (not numeric value order) is okay, and the<br>
lines are fixed width columns, no need to manually split the columns<br>
into tuples; just read all lines into a list and define a key function<br>
that picks out the columns needed<br>
<br>
        data.sort(key=lambda x : x[colstart:colend])<br>
<br>
<br>
If #1 if FALSE (too big for memory) you will need to create a sort-merge<br>
procedure in which you read n-lines of the file; sort them, write to<br>
temporary file; alternating among 2+ temporary files keeping the same<br>
n-lines (except for the last packet). Then merge the 2+ temporaries over<br>
the n-lines in the batch to a new temporary file; after the first n<br>
lines have been merged (giving n*2+ lines in the batch) switch to<br>
another temporary file for the next batch.... When all original batches<br>
are merged, repeat the merge using batches of size n*2+... Repeat until<br>
only one temporary file is left (ie, only one long merge batch is<br>
written).<br>
<br>
        Or figure out how to call whatever system sort command is available<br>
with whatever parameters are needed -- after all, why reinvent the wheel<br>
if you can reach outside the snake and grab that is already in the snake<br>
pit ("outside the snake" => os.system(...); "snake pit" => the OS<br>
environment). Even WinXP has a command line sort command; as long as you<br>
don't need a multikey sort it can handle the simple text record sorting<br>
with limitations on memory size to use.<br>
<font color="#888888"><br>
--<br>
        Wulfraed        Dennis Lee Bieber               KD6MOG<br>
        <a href="mailto:wlfraed@ix.netcom.com">wlfraed@ix.netcom.com</a>           <a href="mailto:wulfraed@bestiaria.com">wulfraed@bestiaria.com</a><br>
                <a href="HTTP://wlfraed.home.netcom.com/" target="_blank">HTTP://wlfraed.home.netcom.com/</a><br>
        (Bestiaria Support Staff:               <a href="mailto:web-asst@bestiaria.com">web-asst@bestiaria.com</a>)<br>
                <a href="HTTP://www.bestiaria.com/" target="_blank">HTTP://www.bestiaria.com/</a><br>
</font><div><div></div><div class="Wj3C7c">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Beema Shafreen