We help IT Professionals succeed at work.

Problem with CSV File

ST3VO
ST3VO asked
on
400 Views
Last Modified: 2010-04-05
Hi all,

I have a problem with a CSV file with I need to sort from my application.

These are the steps in what I need the program to do:

1. Read a CSV File
2. Sort the CSV by .... (This could be different, so I just need to be able to sort the CSV)
3. Save it back to Disk.

Hope you can help.

Thanks

ST3VO
Comment
Watch Question

Author

Commented:
By the way: The sorting would be perfect if I could use an SQL Query but I dont know if it can be done!

Thanks again!
 
Tomas Helgi JohannssonDatabase Administrator / Software Engineer
CERTIFIED EXPERT

Commented:
  Hi!

Here are some CSV examples : http://www.java2s.com/Code/Java/Development-Class/CSV-File.htm
Then for sorting just call Collections.Sort(l) as shown in this
example http://www.java2s.com/Code/Java/Collections-Data-Structure/Sortalist.htm
Here are so some OpenSource libraries to aid you in reading/writing CSV files.
http://opencsv.sourceforge.net/
http://supercsv.sourceforge.net/

Regards,
   Tomas Helgi

Author

Commented:
I need something in Delphi please!

All the samples point to JAVA Code.
Tomas Helgi JohannssonDatabase Administrator / Software Engineer
CERTIFIED EXPERT

Commented:
Hmmm sorry about this confusion :)


Regards,
  Tomas Helgi
Tomas Helgi JohannssonDatabase Administrator / Software Engineer
CERTIFIED EXPERT

Commented:
   
JEDI Project has a component called JTvCustomDataSet  where you
can read CSV file into a TDataSet Descendant, do some sorting and
export it back to a CSV file.

Regards,
  Tomas Helgi
Use TstringList and use Loadfromfile(<filename>) method to load the into stringlist and then sort the TStringlist object. Once u sort Tstringlist use method called Savetofile to save it back with new sort
Tomas Helgi JohannssonDatabase Administrator / Software Engineer
CERTIFIED EXPERT

Commented:
Thats one way to do it. :)
However that will leed to the fact that you have to code sorting routines
for every different CSV file that the program will use in the future.
Using the DataSet descendants gives you more user-friendlier control
over sorting different CSV files.

Regards,
  Tomas Helgi
Tomas Helgi JohannssonDatabase Administrator / Software Engineer
CERTIFIED EXPERT

Commented:
And of course handling the data. :)

Regards,
   Tomas Helgi

Author

Commented:
My problem is that I might have to use a database connection somehow as there will be sort SQL involved.

This is want I need to do:

1. Program Loads
2. <Code connects to the CSV file>
3. User performs a query (e.g: I'll have a couple of Combos 1.All Cars 2.Red Cars
4. Then the <code> will filter this and Sort the data so that the Query results are first
5. Save back to CSV.

Any ideas please?

Thanks again!


Tomas Helgi JohannssonDatabase Administrator / Software Engineer
CERTIFIED EXPERT

Commented:
Use JTvCustomDataSet. That component is ideal for
this as you can use Filter to filter out the desired data
and save it back to CSV file easilly.

Regards,
  Tomas Helgi

Commented:
You can use ADO

Commented:
Any problems with it?

Author

Commented:
I've looked up some connection strings for csv using ADO but none of them work so I keep searching :o)
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Great! Works perfect! Thanks :o)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.