2

Completely new to python i'm having problems finding this with version 2.4

I have a text file that contains 3 columns they are tab delimited by not a single tab, 9 tabs. 1000 line file ex:

$1$sda4356:[sgb1_diska5.ldlbat44.libabl]talild_0329_base.rpt                                talild_0329_base.rpt                                                                      00000000000000005062

What i need to do is sort everything by column 3 ( 00000000000000005062 ).

Is using the csv reader the best way to handle doing this? how do i get it to handle the fact that my text file is using 9 tabs to seperate each column. Most other things i'm finding are incompatiable with python 2.4.

1
  • What do you have so far? Are you reading in the file, or are you stuck on that delimiter? Commented Jan 15, 2013 at 16:29

1 Answer 1

5

Since you're using 9 tabs, str.split seems like it should be able to handle this properly, (but I don't see any reason why csv would choke on it ...):

fobj = open('data.csv')
data = [ x.split() for x in fobj ]
fobj.close()  #don't forget to close :)

Or possibly:

delim = '\t'*9
data = [ x.split(delim) for x in fobj ]

Then you can sort that. In a newer version of python, you'd do:

from operator import itemgetter
data.sort(key=itemgetter(2))

Try this first! (According to the comments and the documentation, key is supported in python2.4, but not python2.3). operator.itemgetter is also "new in python2.4" and itemgetter(2) is equivalent to lambda x:x[2] or the more verbose:

def get2(x):
    return x[2]

But I think you might need to rely on cmp for compatability with really old python versions (Note, that python3 no longer supports cmp):

def compare(x1,x2):
    if x1[2] > x2[2]:
       return 1
    elif x1[2] < x2[2]:
       return -1
    else:
       return 0

and then you'd sort:

data.sort(cmp=compare)
Sign up to request clarification or add additional context in comments.

7 Comments

Python 2.4 supports key.
@J.F.Sebastian -- Found the documentation to prove your assertion. python2.4 does in fact support key, but python2.3 doesn't. Thanks for pointing this out. (It seems like a lot of good things happened in python2.4 ...)
fobj = open('data.csv') data = [ x.split() for x in fobj ] fobj.close() from operator import itemgetter data.sort(key=itemgetter(2)) def get2(x): return x[2] def compare(x1,x2): if x1[2] > x2[2]: return 1 elif x1[2] < x2[2]: return -1 else: return 0 data.sort(cmp=compare) What am i doing wrong ? Traceback (most recent call last): File "test.py", line 8, in ? data.sort(key=itemgetter(2)) IndexError: list index out of range Thanks!
tried both ways with the from operator import itemgetter data.sort(key=itemgetter(2)) and def compare(x1,x2): if x1[2] > x2[2]: return 1 elif x1[2] < x2[2]: return -1 else: return 0 both give me the same error i think i'm missing something with the x.split maybe.. IndexError: list index out of range
you probably have a line with only one column to see which do for d in data:if len(d) <2:print(d)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.