Alternative delimiters for CSV files?
Alternative delimiters for CSV files?
Author
Discussion

judas

Original Poster:

6,240 posts

289 months

Thursday 19th May 2005
quotequote all
Need to dump some data out from a website in CSV format, which is easy enough; problem I have is when the data itself contains commas - naturally, excel interprets these as a delimiter and breaks to a new column...

I've tried using vbtab as a delimiter but that doesn't work.

Any ideas?

pdV6

16,442 posts

291 months

Thursday 19th May 2005
quotequote all
Being pedantic, CSV stands for "comma separted values", so the exact answer is "no you can't use different separators".

Practically speaking, you hit this sort of problem all the time. 3 options:

1. Remove commas from the data before creating the csv file (or replace them with something else that the application(s) that read the file will know to substitute commas for)

2. Surround each piece of data with delimeters (e.g. " quotes) and, again, get the app that reads the data to sort it out.

3. Pick another delimeter that doesn't appear in the data, but call the file something else other than csv...

Plotloss

67,280 posts

300 months

Thursday 19th May 2005
quotequote all
Some imports allow the use of any delimiter.

iSeries data import for example and DTS (I think)

~ (tilda) delimited is popular...

Don

28,378 posts

314 months

Thursday 19th May 2005
quotequote all
What Plotters said.

pdV6

16,442 posts

291 months

Thursday 19th May 2005
quotequote all
pdV6 said:

2. Surround each piece of data with delimeters (e.g. " quotes) and, again, get the app that reads the data to sort it out.

Actually, just tried a few experiments with Excel and the above seems to work best. Excel sorts it out for you.

NB Probably only need the quotes around text fields, which has the side-effect of forcing string formatting onto the affected cells.

judas

Original Poster:

6,240 posts

289 months

Thursday 19th May 2005
quotequote all
I can make the delimiter whatever I like, but unless Excel recognises it as a delimiter it just dumps it in as a single text string.

>> Edited by judas on Thursday 19th May 16:55

Plotloss

67,280 posts

300 months

Thursday 19th May 2005
quotequote all
Does Excel not prompt you with a box that has radio buttons and an edit box to put in the delimiter thats being used?

To trigger this, the extension of the file needs to be csv even if its not a csv in the proper sense.

judas

Original Poster:

6,240 posts

289 months

Thursday 19th May 2005
quotequote all
Excel just opens the file without any prompts, whether I launch it from the website directly, or download it and them open it.

Tring the pdv6 enclose in quotes method now. I hate escaping that many quotes...

>> Edited by judas on Thursday 19th May 17:04

Plotloss

67,280 posts

300 months

Thursday 19th May 2005
quotequote all
Tried Data>Import Data?

That should prompt you for teh format...

ErnestM

11,621 posts

297 months

Thursday 19th May 2005
quotequote all
Import the data into Access first, then export the table as a CSV. Access allows virtually anything to be used as a delimiter - spaces, semicolon, commas, whatever...

ErnestM

judas

Original Poster:

6,240 posts

289 months

Thursday 19th May 2005
quotequote all
This is a client site - has to be an idiot-proof, one click solution; hence to Access or complicated instructions for the client (they have enough dificulty with the basics )

pdV6

16,442 posts

291 months

Thursday 19th May 2005
quotequote all
Plotloss said:
Tried Data>Import Data?

That should prompt you for teh format...

That's the fella - allows you to fiddle as the data's coming in. Commas and quotes allow you to just double click the file, though, as they're the defaults...

judas

Original Poster:

6,240 posts

289 months

Thursday 19th May 2005
quotequote all
Bah! Looks like I've actually got some bad data which is screwing up the CSV generation anyhow.

Back to the drawing board...

I think the programmer who came up with the database schema was either smoking something pretty potent, or is a sadistic bastard who likes making my life a misery. Either way there's gonna be a kickin' tomorrow...

>> Edited by judas on Thursday 19th May 17:43

pdV6

16,442 posts

291 months

Friday 20th May 2005
quotequote all
judas said:
there's gonna be a kickin' tomorrow...

ATG

23,801 posts

302 months

Friday 20th May 2005
quotequote all
Trying to pick a magic delimiter character which never, ever, ever appears in the data is pants, because it always crops up eventually and then there is misery, gnashing of hair, wailing of teeth and, uhm, ripping ... think i need more coffee.

Each data item in a CSV should be separated by a comma. Any data item that is a text string should be enclosed in quotation marks. Fine so far, but what happens if you want your text to contain quotation marks too? You need another rule. Two possibilities are:

Rule 1:
If "" appears inside a text field it doesn't end the string but should be interpretted as a single " in the string. Thus ,"", is an empty string and ," "" ", is a space followed by a quotation mark followed by another space, and ,"""", is a single quotation mark.

Rule 2:
Use an "escape" character to indicate that the following character is never to be considered a delimiter. Common choice is backslash (\). This method also means you don't have to delimit strings in quotation marks though many people choose to do it anyway as it avoids confusion btwn numbers and text. If you wanted to a text field containing a single comma, you would enter it like this ,\,, or like this ,"\,", if you keep the quotation marks in. If you want to have a backslash in the text, you enter \\ as the first backslash indicates that the second one is just a peice of text.

editted because all my backslashes dissapeared as they are escape chars in HTML ... duh

>> Edited by ATG on Friday 20th May 10:28

judas

Original Poster:

6,240 posts

289 months

Friday 20th May 2005
quotequote all
Developer duly kicked - I've made him rewrite the query to pull the data out

As for delimiters, the dataset possibiities are finite - just items from fixed lists of text and numeric scores - so I can work around that as long as Excel is ok with the delimiters. I've enclosed everything in quotes, which works ok if I save the file and then open it. But if I try to open it directly by streaming it from the website Excel doesn't interpret it properly

Ah, I'll get there eventually...

pdV6

16,442 posts

291 months

Friday 20th May 2005
quotequote all
One thing to note, though, is that Excel will treat numerics in quotes as strings. May not bother you, but if you're then going on to work with the figures, it could cause a headache later.

judas

Original Poster:

6,240 posts

289 months

Friday 20th May 2005
quotequote all
That's the client's problem Getting the file into excel is only an intermediate step anyhow - it then gets imported in SPSS for statistical analysis. We've made it clear there's only so much hand-holding we can do for them.