The Artima Developer Community
Sponsored Link

Java Answers Forum
sort CSV file

3 replies on 1 page. Most recent reply: Feb 14, 2004 3:40 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Yuliya

Posts: 1
Nickname: yulia
Registered: Feb, 2004

sort CSV file Posted: Feb 9, 2004 3:54 PM
Reply to this message Reply
Advertisement
As usual, I'm stuck. CSV file is massive (181 MB). How can I sort it in a post-code order (which is the third column)
Please help
Thank you
Julia


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: sort CSV file Posted: Feb 9, 2004 5:53 PM
Reply to this message Reply
This would do it:

def SortCsvByField( filename, fieldNo, sep = ',' ):
records = [line.split(sep) for line in file(filename)]
records.sort( lambda a,b: cmp(a[fieldNo],b[fieldNo]) )
f = file(filename,'w')
for rec in records:
f.write( sep.join(rec) )

SortCsvByField( 'somefile.csv', 2, ',' )


...but this is Python code, not Java. So you could do one of the following things with it:

1) Download Python and use it.

2) Download Jython and use that.

3) Look at this as a pseudo code starting point and write the same thing in Java.

4) Wait for someone to write a Java answer.

(you might want to add a little error handling, as well -- in particular, you may want to filter out lines that don't match the cvs format, don't have enough fields, are emtpy, or what have you).

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: sort CSV file Posted: Feb 10, 2004 2:31 PM
Reply to this message Reply
Oh now I know where you picked up my email address from!

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: sort CSV file Posted: Feb 14, 2004 3:40 PM
Reply to this message Reply
The big file you have probably has a record of data for each line of the file.

BufferedReader is ideal for reading a whole line of character data in from a file.

Then you process each line to separate the fields using the delimiter character or characters.

You can make up a simple data holder class which holds one record with string fields or private String variables and you can get fancy and use good class design with get and set methods to control the fields and obtain data.

Add a comparator interface to the data holder class to sort or compare one object's third field with anoy other objects, third field.

Read each record into a List which contains all these data holder record objects.

Use Collections.sort(yourlist);

and then do whatever you needed to have done next.

If the data you need to process is in a database, it is even easier to process it using a jdbc connection to the database.

Flat View: This topic has 3 replies on 1 page
Topic: OutOfMemoryException in java Previous Topic   Next Topic Topic: Program Flow: try, catch, finally

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use