Question : Delphi: Importing CSV file that has new line characters

I am able to import CSV file by using stringlist's commatext property,
However, in my case my field values also have new line character in it
e.g.
"TelNo", "Address"
"1", "c6/2 // 1st record started
abcd"   // 1st record ended
"2", "abcd, // 2nd record started
bbdggzsdh,
ajgfhjg"  // 2nd record ended.

String list can not recognize new line character, hence, i am not able to import such file.
Related Solutions: Delphi Parse a csv

Answer : Delphi: Importing CSV file that has new line characters

@prakashmote

* Are these columnar new line characters a single character, CR or LF?
* do the CSV records end with a double character CRLF?

=============
If the columnar new lines are a single character and the record delimiters are double characters, then you can do a version of Geert's method (http:#32696374).

1. read the entire contents into memory (filestream.loadfromfile)
2. replace all end of record character doublets with some string that does not occur elsewhere in the data. (must not include either CR or LF characters,#D or #A)
3. replace all the columnar new line characters with a different string (than step 2)
4. Undo step 2
5. transfer the lines to a tstringlist data structure, so that each line is an item in the tstringlist.

For each record,
6. parse the columnar data
7. for each parsed column, undo step 3, restoring the new line characters.
8. add the record to the database.

Notes:
* you might skip step 7 and do the replacement after the rows are in the database.
* since the columnar data contains commas, you will not be able to use the tstringlist delimited text feature to do the parsing.
Random Solutions  
 
programming4us programming4us