Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

sorting a file

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


micyaro at pf

Dec 5, 2002, 9:08 AM

Post #1 of 7 (4907 views)
Permalink
sorting a file

Hello,

I am trying to rewrite a little shell script into python.
I ve got a text file with following content:

foo bar file_name1.ext ./foobar
foo bar file_name2.ext ./foobar
...
..
.


Now the shell script reads only the "file_nameXX.ext"-part and using
it as a variable, the script moves the file "file_nameXX.ext" from a
directory to another directory given. It looks like this:

source_dir = /foo/bar
dest_dir = /another/foo/bar
sourcefile = "source_dir" + "/" + "file_name"
destfile = "dest_dir" + "/" + "file_name"
PROGRAM = move sourcefile destfile

Does anybody could tell me how the lines above would look like in
python?
Thank you for helping me out.

Best regards,
Michel


gminick at hacker

Dec 5, 2002, 9:57 AM

Post #2 of 7 (4841 views)
Permalink
sorting a file [In reply to]

Dnia 5 Dec 2002 08:08:16 -0800, Romualdo napisał(a):
> Does anybody could tell me how the lines above would look like in
> python?
It's not exactly what you want, but should be helpful.
Our file (~/plik):
---
/home/gminick/text /home/gminick/text.txt
/home/gminick/blah /home/gminick/blah.txt
---

Our script:
---
import re, os

w = open("plik")

a = w.readline()
while a:
b = re.search("([\w\/\.]+)\s+([\w\/\.]+)", a)
if b:
b = b.groups()
os.system("mv %s %s" % (b[0], b[1]))
a = w.readline()
---

It reads lines from file "plik" and searching for two path(/file)names,
if found it moves a file from a name found in first field, to that found
in second.
So, here we're moving two files: text to text.txt and blah to blah.txt.
HTH.

--
[ ] gminick (at) underground.org.pl http://gminick.linuxsecurity.pl/ [ ]
[ "Po prostu lubie poranna samotnosc, bo wtedy kawa smakuje najlepiej." ]


tjreedy at udel

Dec 5, 2002, 3:34 PM

Post #3 of 7 (4839 views)
Permalink
sorting a file [In reply to]

"Romualdo" <micyaro [at] pf> wrote in message
news:946575c0.0212050808.70c1e246 [at] posting
> Hello,
>
> I am trying to rewrite a little shell script into python.
> I ve got a text file with following content:
>
> foo bar file_name1.ext ./foobar
> foo bar file_name2.ext ./foobar
> ...
> Now the shell script reads only the "file_nameXX.ext"-part and using
> it as a variable, the script moves the file "file_nameXX.ext" from a
> directory to another directory given. It looks like this:
>
> source_dir = /foo/bar
> dest_dir = /another/foo/bar
> sourcefile = "source_dir" + "/" + "file_name"
> destfile = "dest_dir" + "/" + "file_name"
> PROGRAM = move sourcefile destfile
>
> Does anybody could tell me how the lines above would look like in
> python?
> Thank you for helping me out.

I would do something like (UNTESTED!, but gives idea):

import shutil
for line in file('movelist.txt'):
d1,d2,f,t = line.split() # assuming all lines have 4 fields
src = '/%s/%s/%s' % (d1,d2,f)
dst = '/another/%s/%s/%s' % (d1,d2,f) # its not clear what
'./foobar/ is about
shutil.filecopy(src,dst) #not sure of method name

TJR


inq1ltd at inqvista

Jun 14, 2008, 9:00 AM

Post #4 of 7 (4838 views)
Permalink
Re: sorting a file [In reply to]

On Saturday 14 June 2008 03:15, Beema
shafreen wrote:
> Hi all,
>
> I have a file with three columns i need
> to sort the file with respect to the third
> column. How do I do it uisng python. I
> used Linux command to do this. Sort but i
> not able to do it ? can any body ssuggest
> me


I have used this method to solve similar
problems.

This is a consept of how to do what you want,
but you will have to work a little to get it
right.

You might try something like this;

Dict = {} ##create a dictionary

make a list of all column3 values

for loop colum3 values

Make these values the key in a dictionary
If the values are long, you can use the first
7 to 15 characters if you want.

use this key to equal all the values in the
other columns on the same row.

Dict[column3] = column1, column2, column3

once the dictionary is made

get the dictionary key
x = Dict.keys() ## get the keys from Dict


x.sort() # produce a sorted list of keys of
column3

Loop these sorted keys to extract from the
dictionary the values related to each


jim-on-linux
http://:inqvista.com













--
http://mail.python.org/mailman/listinfo/python-list


mark.hachey at gmail

Jun 14, 2008, 2:33 PM

Post #5 of 7 (4816 views)
Permalink
Re: sorting a file [In reply to]

On Jun 14, 12:00 pm, jim-on-linux <inq1...@inqvista.com> wrote:
> On Saturday 14 June 2008 03:15, Beema
>
> shafreen wrote:
> > Hi all,
>
> > I have a file with three columns i need
> > to sort the file with respect to the third
> > column. How do I do it uisng python. I
> > used Linux command to do this. Sort but i
> > not able to do it ? can any body ssuggest
> > me
>
> I have used this method to solve similar
> problems.
>
> This is a consept of how to do what you want,
> but you will have to work a little to get it
> right.
>
> You might try something like this;
>
> Dict = {} ##create a dictionary
>
> make a list of all column3 values
>
> for loop colum3 values
>
> Make these values the key in a dictionary
> If the values are long, you can use the first
> 7 to 15 characters if you want.
>
> use this key to equal all the values in the
> other columns on the same row.
>
> Dict[column3] = column1, column2, column3
>
> once the dictionary is made
>
> get the dictionary key
> x = Dict.keys() ## get the keys from Dict
>
> x.sort() # produce a sorted list of keys of
> column3
>
> Loop these sorted keys to extract from the
> dictionary the values related to each
>
> jim-on-linux
> http://:inqvista.com


Here's another way to attack it. Make a class that takes your columns
as arguments. define an operator on that class to get the comparison
you want. make a list that is your rows, and call sort. here's an
example using only two columns

class twoColumns :
def __init__(self, c1 = 0, c2 = 0) :
self.c1 = c1
self.c2 = c2

#just use this for debugging if you want to see everything
def getVals(self):
print self.c1, self.c2

#order members of this class by the second column
def __lt__(c,d):
if (c.c2 < d.c2) :
return 1
else :
return 0

#end class definition

Here's what happened when I used this class.

>>> r1 = twoColumns(3,4)
>>> r2 = twoColumns(1,5)
>>> test = [r1,r2] #this is already sorted
>>> test[0].getVals()
3 4
>>> test[1].getVals()
1 5
>>> test.sort() #and the sort looks the same
>>> test[0].getVals()
3 4
>>> test[1].getVals()
1 5
>>> test = [r2,r1] #I reversed the rows
>>> test[0].getVals()
1 5
>>> test[1].getVals()
3 4
>>> test.sort() #and we get what we want
>>> test[0].getVals()
3 4
>>> test[1].getVals()
1 5

I think that does what you're asking.
--
http://mail.python.org/mailman/listinfo/python-list


sjmachin at lexicon

Jun 14, 2008, 3:18 PM

Post #6 of 7 (4824 views)
Permalink
Re: sorting a file [In reply to]

On Jun 15, 2:00 am, jim-on-linux <inq1...@inqvista.com> wrote:
> On Saturday 14 June 2008 03:15, Beema
>
> shafreen wrote:
> > Hi all,
>
> > I have a file with three columns i need
> > to sort the file with respect to the third
> > column. How do I do it uisng python. I
> > used Linux command to do this. Sort but i
> > not able to do it ? can any body ssuggest
> > me
>
> I have used this method to solve similar
> problems.
>
> This is a consept of how to do what you want,
> but you will have to work a little to get it
> right.
>
> You might try something like this;
>
> Dict = {} ##create a dictionary
>
> make a list of all column3 values

Ummm this appears somewhat baroque .... try this:

Assuming the OP has arranged to read his data into a list of lists
"filedata" such that filedata[-1][2] is the data in the 3rd column of
the last row. Further assume that the data has just been read with
e.g. the csv module so all data is still in text form and may need
some conversion before comparison (otherwise e.g. a salary of 100000
would appear before a salary of 99999).

Suppose the 3rd column is a salary, without '$' and ',' in it.

Now let's look at the manual for the sort method. It gives us 2
options: a cmp function and a key function, with a strong hint that
key is better. So for each row, what do we want on sort on? It's the
numerical value of the 3rd item:

def sort_key_func(row):
return float(row[2])

Then we can simply do:
filedata.sort(key=sort_key_func)

All that remains is to write the contents of filedata to the output
file.

HTH,
John
--
http://mail.python.org/mailman/listinfo/python-list


beema.shafreen at gmail

Jun 14, 2008, 11:38 PM

Post #7 of 7 (4815 views)
Permalink
Re: sorting a file [In reply to]

Thanks lot for your valuable suggestions

On Sun, Jun 15, 2008 at 4:04 AM, Dennis Lee Bieber <wlfraed [at] ix>
wrote:

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



--
Beema Shafreen

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.