buley
buley

Reputation: 29208

Is there a way to include commas in CSV columns without breaking the formatting?

I've got a two column CSV with a name and a number. Some people's name use commas, for example Joe Blow, CFA. This comma breaks the CSV format, since it's interpreted as a new column.

I've read up and the most common prescription seems to be replacing that character, or replacing the delimiter, with a new value (e.g. this|that|the, other).

I'd really like to keep the comma separator (I know excel supports other delimiters but other interpreters may not). I'd also like to keep the comma in the name, as Joe Blow| CFA looks pretty silly.

Is there a way to include commas in CSV columns without breaking the formatting, for example by escaping them?

Upvotes: 641

Views: 829756

Answers (20)

Ryan
Ryan

Reputation: 28187

To encode a field containing comma (,) or double-quote (") characters, enclose the field in double-quotes:

field1,"field, 2",field3, ...

Literal double-quote characters are typically represented by a pair of double-quotes (""). For example, a field exclusively containing one double-quote character is encoded as """".

For example:

Sheet: |Hello, World!|You "matter" to us.|

  CSV: "Hello, World!","You ""matter"" to us."

More examples (sheet → csv):

  • regular_valueregular_value
  • Fresh, brown "eggs""Fresh, brown ""eggs"""
  • """""
  • ","""","""
  • ,,,"",,,"""
  • ,"",","""","
  • """""""""""

See Wikipedia.

Upvotes: 852

albertochigua
albertochigua

Reputation: 55

An “official” format would be:

https://www.ietf.org/rfc/rfc4180.txt

  1. Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes. For example:
   "aaa","b CRLF
   bb","ccc" CRLF
   zzz,yyy,xxx

Upvotes: 2

Progga Ilma
Progga Ilma

Reputation: 635

Just add the quotation to the string like this data = '"'+data+'"' this worked for me on ReactJS.

Upvotes: -1

micans
micans

Reputation: 1106

Tab-separated (tsv) files are superior to csv and are supported and understood by Excel and other applications. It is highly puzzling that csv files are still in use. Wherever possible use tsv. Commas, double quotes and semi-colons all tend to occur in text (leading to a quagmire of non-standardised encoding conventions), <TAB> characters very much not so. Although not an answer to the question itself, it is worthwhile pointing out for those coming here for whom escaping from csv files is still an option.

Upvotes: 1

Squibly
Squibly

Reputation: 447

A C# method for escaping delimiter characters and quotes in column text. It should be all you need to ensure your csv is not mangled.

private string EscapeDelimiter(string field)
    {
        if (field.Contains(yourEscapeCharacter))
        {
            field = field.Replace("\"", "\"\"");
            field = $"\"{field}\"";
        }

        return field;
    }

Upvotes: 0

RasTheDestroyer
RasTheDestroyer

Reputation: 1764

This isn't a perfect solution, but you can just replace all uses of commas with or a lower quote. It looks very very similar to a comma and will visually serve the same purpose. No quotes are required

in JS this would be

stringVal.replaceAll(',', '‚')

You will need to be super careful of cases where you need to directly compare that data though

Upvotes: 5

Ahmed Mohammed
Ahmed Mohammed

Reputation: 537

CSV files can actually be formatted using different delimiters, comma is just the default.

You can use the sep flag to specify the delimiter you want for your CSV file.

Just add the line sep=; as the very first line in your CSV file, that is if you want your delimiter to be semi-colon. You can change it to any other character.

Example

Upvotes: 12

SLaks
SLaks

Reputation: 887453

You need to quote that values.
Here is a more detailed spec.

Upvotes: 14

Bal Krishna Jha
Bal Krishna Jha

Reputation: 7206

You can use Template literals (Template strings)

e.g -

`"${item}"`

Upvotes: 8

mudin
mudin

Reputation: 2852

I found that some applications like Numbers in Mac ignore the double quote if there is space before it.

a, "b,c" doesn't work while a,"b,c" works.

Upvotes: 77

user25307
user25307

Reputation: 41

May not be what is needed here but it's a very old question and the answer may help others. A tip I find useful with importing into Excel with a different separator is to open the file in a text editor and add a first line like:

sep=|

where | is the separator you wish Excel to use. Alternatively you can change the default separator in Windows but a bit long-winded:

Control Panel>Clock & region>Region>Formats>Additional>Numbers>List separator [change from comma to your preferred alternative]. That means Excel will also default to exporting CSVs using the chosen separator.

Upvotes: 2

Kevin
Kevin

Reputation: 121

You could encode your values, for example in PHP base64_encode($str) / base64_decode($str)

IMO this is simpler than doubling up quotes, etc.

https://www.php.net/manual/en/function.base64-encode.php

The encoded values will never contain a comma so every comma in your CSV will be a separator.

Upvotes: 1

mathengineer
mathengineer

Reputation: 160

Double quotes not worked for me, it worked for me \". If you want to place a double quotes as example you can set \"\".

You can build formulas, as example:

fprintf(strout, "\"=if(C3=1,\"\"\"\",B3)\"\n");

will write in csv:

=IF(C3=1,"",B3)

Upvotes: 0

amiksch
amiksch

Reputation: 82

Depending on your language, there may be a to_json method available. That will escape many things that break CSVs.

Upvotes: 4

LifeiSHot
LifeiSHot

Reputation: 139

First, if item value has double quote character ("), replace with 2 double quote character ("")

item = item.ToString().Replace("""", """""")

Finally, wrap item value:

ON LEFT: With double quote character (")

ON RIGHT: With double quote character (") and comma character (,)

csv += """" & item.ToString() & ""","

Upvotes: 0

Mohammed
Mohammed

Reputation: 1394

I faced the same problem and quoting the , did not help. Eventually, I replaced the , with +, finished the processing, saved the output into an outfile and replaced the + with ,. This may seem ugly but it worked for me.

Upvotes: 2

Cloud
Cloud

Reputation: 1034

If you want to make that you said, you can use quotes. Something like this

$name = "Joe Blow, CFA.";
$arr[] = "\"".$name."\"";

so now, you can use comma in your name variable.

Upvotes: 23

hlongmore
hlongmore

Reputation: 1846

In addition to the points in other answers: one thing to note if you are using quotes in Excel is the placement of your spaces. If you have a line of code like this:

print '%s, "%s", "%s", "%s"' % (value_1, value_2, value_3, value_4)

Excel will treat the initial quote as a literal quote instead of using it to escape commas. Your code will need to change to

print '%s,"%s","%s","%s"' % (value_1, value_2, value_3, value_4)

It was this subtlety that brought me here.

Upvotes: 12

sakshi jain
sakshi jain

Reputation: 331

You can use the Text_Qualifier field in your Flat file connection manager to as ". This should wrap your data in quotes and only separate by commas which are outside the quotes.

Upvotes: 0

Rudu
Rudu

Reputation: 15892

The problem with the CSV format, is there's not one spec, there are several accepted methods, with no way of distinguishing which should be used (for generate/interpret). I discussed all the methods to escape characters (newlines in that case, but same basic premise) in another post. Basically it comes down to using a CSV generation/escaping process for the intended users, and hoping the rest don't mind.

Reference spec document.

Upvotes: 40

Related Questions