Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 06-18-2012, 05:49 PM
Ouiji Ouiji is offline
Senior Member
 
Join Date: Nov 2001
Location: US of A
Posts: 478
Send a message via AIM to Ouiji
Default Reading CSV file with extra commas inside quotes..

I have found several posts about reading .csv file which are comma delineated. Unfortunately none of them take into account that when data inside the csv file has a comma in it already, it is places into Quotes "". This keeps throwing off my import and I can't seem to get past it.. any ideas?

Thanks,
-ouiji

Example:
Account Name,Type,Date,Product

"Delphi Pages, INC",Web Based Help,06/16/2012,Delphi Help

[The comma already in the name throws off the import]
__________________
"not quite smart enough to be dumb"
Extended Stats No Longer Available Due To Changes To The Forum.
  #2  
Old 06-18-2012, 10:20 PM
Marsheng Marsheng is offline
Senior Member
 
Join Date: Nov 2008
Posts: 205
Default

This is a long way round but use the idea for your own formula

In your input line, search for "
then truncate your input line up to and including the "
search for " again then delete all data after and including "
you now have the text left. Search for , and replace it with something never used eg &*.
Now recreate the original string with the new string created and do the import.

When complete search through the imported data and replace &* with ,

If there are more than one set of " in the original string you will have to go through each one.
  #3  
Old 06-18-2012, 11:29 PM
Ouiji Ouiji is offline
Senior Member
 
Join Date: Nov 2001
Location: US of A
Posts: 478
Send a message via AIM to Ouiji
Default

I appreciate the reply, I tried something similar to what you are suggesting, but it was going character by character changing the commas inside the quotes to another unused character like |. Unfortunately the csv files are all 7-12mb+ and the time it takes to load and process is ridiculous. I will give your method a try and see if it comes out any quicker.

Thanks again,
- ouiji
__________________
"not quite smart enough to be dumb"
Extended Stats No Longer Available Due To Changes To The Forum.
  #4  
Old 06-19-2012, 12:28 AM
Marsheng Marsheng is offline
Senior Member
 
Join Date: Nov 2008
Posts: 205
Default

The only way to make it quicker is to make some valid assumptions.
If no " in source data, go to next record/line
if " and no, go to next record/line
If there is only one string per record/line, once you have done the &* replace go to the next line of source data.

7-12 mb That is what computers are good for.
  #5  
Old 06-19-2012, 07:40 AM
Donovan Donovan is offline
Senior Member
 
Join Date: Jan 2011
Posts: 150
Send a message via Skype™ to Donovan
Default

The Jedi Component Set has a component that would be perfect here called a JVCSVDataset.

It has the ability to load a CSV file and will ignore any commas inside inverted commas, check it out.

http://www.delphi-jedi.org/
  #6  
Old 06-19-2012, 09:58 AM
OldTurtle OldTurtle is offline
Junior Member
 
Join Date: Apr 2010
Posts: 18
Default

I found this procedure in Internet (I had the same problem) and works well.

procedure ReadCSVRecord(Line : string; Strings : TStringList);
var P : PChar;
Field : string;
Quote : boolean;
begin
Strings.Clear;
if length(Line) > 0 then
begin
P := @Line[1];
repeat
Field := '';
if P^ = '"' then
begin
Inc(P);
repeat
while P^ <> '"' do
begin
Field := Field + P^;
Inc(P);
end;
Quote := False;
while P^ = '"' do
begin
Inc(P);
if Quote then Field := Field + '"';
Quote := not Quote;
end;
until (P^ = #0) or Quote;
end
else
begin
while (P^ <> #0) and (P^ <> ',') do
begin
Field := Field + P^;
Inc(P);
end;
end;
Strings.Add(Field);
if P^ = #0 then exit;
Inc(P);
until P^ = #0;
Strings.Add('');
end;
end;
  #7  
Old 06-20-2012, 02:51 PM
Ouiji Ouiji is offline
Senior Member
 
Join Date: Nov 2001
Location: US of A
Posts: 478
Send a message via AIM to Ouiji
Default

Thanks everyone, really appreciate the replies. Everything is working great.

- ouiji
__________________
"not quite smart enough to be dumb"
Extended Stats No Longer Available Due To Changes To The Forum.
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 11:32 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.