486

I am looking for suggestions on how to handle a csv file that is being created, then uploaded by our customers, and that may have a comma in a value, like a company name.

Some of the ideas we are looking at are: quoted Identifiers (value "," values ","etc) or using a | instead of a comma. The biggest problem is that we have to make it easy, or the customer won't do it.

Svante
  • 46,788
  • 11
  • 77
  • 118
Bob The Janitor
  • 19,164
  • 9
  • 45
  • 71
  • the customer is writing it and uploading it – Bob The Janitor Apr 20 '09 at 19:42
  • 1
    Here is the solution to manage inside commos in csv file. visit https://stackoverflow.com/questions/9889225/parse-text-file-and-remove-commas-inside-double-quotes/24902225#24902225 – Hasan Abrar Jul 23 '14 at 05:10
  • on iOS, essentially you **must use** https://github.com/Flinesoft/CSVImporter – Fattie Feb 15 '17 at 17:34
  • 3
    Note that this QA is old. Nowadays **csv means RFC 4180** and that's that. – Fattie Feb 15 '17 at 17:35
  • I have the exact same issue, trying to total a column in a csv file which is comma separated. No problem with an awk command. Unfortunately some cells may contain commas (in an address for example), other cells won't. Looking for a Linux compatible solution but not sure where to start. – greenage Dec 08 '19 at 23:58

27 Answers27

404

For 2017, csv is fully specified - RFC 4180.

It is a very common specification, and is completely covered by many libraries (example).

Simply use any easily-available csv library - that is to say RFC 4180.


There's actually a spec for CSV format and how to handle commas:

Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes.

http://tools.ietf.org/html/rfc4180

So, to have values foo and bar,baz, you do this:

foo,"bar,baz"

Another important requirement to consider (also from the spec):

If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote. For example:

"aaa","b""bb","ccc"
Fattie
  • 30,632
  • 54
  • 336
  • 607
Corey Trager
  • 21,253
  • 16
  • 78
  • 121
  • 123
    "Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes." – Eli Apr 20 '09 at 19:11
  • 43
    "If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote." – C. Dragon 76 Aug 25 '11 at 16:15
  • 11
    Not really a spec, but still probably handy. It says... "There is no formal specification in existence, which allows for a wide variety of interpretations of CSV files. This section documents the format that seems to be followed by most implementations." – Justin Clarke Oct 19 '11 at 13:26
  • 6
    Also, don't forget that, despite its name, CSV values in the row may be separated not only by commas - at least on Windows platforms. It depends on the current regional settings (intl.cpl in command line, "Advanced settings"), in particular, list separator: `System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator`. – lxa Aug 26 '13 at 14:29
  • 4
    Please put relevant information in this answer, in addition to the link, to A) Remove most of the above comments (and mine), B) Save so many more people than the answerer the time to go to yet another page and find the relevant data, C) Prevent Link Rot. – user66001 Jul 02 '15 at 16:22
230

As others have said, you need to escape values that include quotes. Here’s a little CSV reader in C♯ that supports quoted values, including embedded quotes and carriage returns.

By the way, this is unit-tested code. I’m posting it now because this question seems to come up a lot and others may not want an entire library when simple CSV support will do.

You can use it as follows:

using System;
public class test
{
    public static void Main()
    {
        using ( CsvReader reader = new CsvReader( "data.csv" ) )
        {
            foreach( string[] values in reader.RowEnumerator )
            {
                Console.WriteLine( "Row {0} has {1} values.", reader.RowIndex, values.Length );
            }
        }
        Console.ReadLine();
    }
}

Here are the classes. Note that you can use the Csv.Escape function to write valid CSV as well.

using System.IO;
using System.Text.RegularExpressions;

public sealed class CsvReader : System.IDisposable
{
    public CsvReader( string fileName ) : this( new FileStream( fileName, FileMode.Open, FileAccess.Read ) )
    {
    }

    public CsvReader( Stream stream )
    {
        __reader = new StreamReader( stream );
    }

    public System.Collections.IEnumerable RowEnumerator
    {
        get {
            if ( null == __reader )
                throw new System.ApplicationException( "I can't start reading without CSV input." );

            __rowno = 0;
            string sLine;
            string sNextLine;

            while ( null != ( sLine = __reader.ReadLine() ) )
            {
                while ( rexRunOnLine.IsMatch( sLine ) && null != ( sNextLine = __reader.ReadLine() ) )
                    sLine += "\n" + sNextLine;

                __rowno++;
                string[] values = rexCsvSplitter.Split( sLine );

                for ( int i = 0; i < values.Length; i++ )
                    values[i] = Csv.Unescape( values[i] );

                yield return values;
            }

            __reader.Close();
        }
    }

    public long RowIndex { get { return __rowno; } }

    public void Dispose()
    {
        if ( null != __reader ) __reader.Dispose();
    }

    //============================================


    private long __rowno = 0;
    private TextReader __reader;
    private static Regex rexCsvSplitter = new Regex( @",(?=(?:[^""]*""[^""]*"")*(?![^""]*""))" );
    private static Regex rexRunOnLine = new Regex( @"^[^""]*(?:""[^""]*""[^""]*)*""[^""]*$" );
}

public static class Csv
{
    public static string Escape( string s )
    {
        if ( s.Contains( QUOTE ) )
            s = s.Replace( QUOTE, ESCAPED_QUOTE );

        if ( s.IndexOfAny( CHARACTERS_THAT_MUST_BE_QUOTED ) > -1 )
            s = QUOTE + s + QUOTE;

        return s;
    }

    public static string Unescape( string s )
    {
        if ( s.StartsWith( QUOTE ) && s.EndsWith( QUOTE ) )
        {
            s = s.Substring( 1, s.Length - 2 );

            if ( s.Contains( ESCAPED_QUOTE ) )
                s = s.Replace( ESCAPED_QUOTE, QUOTE );
        }

        return s;
    }


    private const string QUOTE = "\"";
    private const string ESCAPED_QUOTE = "\"\"";
    private static char[] CHARACTERS_THAT_MUST_BE_QUOTED = { ',', '"', '\n' };
}
Rory O'Kane
  • 25,436
  • 11
  • 86
  • 123
harpo
  • 37,686
  • 13
  • 90
  • 126
  • 2
    You may also need to translate \r\n for windows compliance, depending on your application. – Mandrake Feb 29 '12 at 17:51
  • 3
    @NadaNaeem, care to elaborate? – harpo Jul 07 '15 at 18:30
  • it is not counting the items in a csv file row correctly, it is not dealing well with the commas in fields and courage returns and taps – Nada N. Hantouli Jul 08 '15 at 08:05
  • Your solution works good except in some rare situations, I've provided an answer with detail as to why. – MikeDub Nov 29 '16 at 21:49
  • .Net actually already contains a csv reader, namely [`TextFieldParser` in the `Microsoft.VisualBasic.FileIO` namespace](https://stackoverflow.com/a/48809517/395685). – Nyerguds Apr 17 '18 at 07:29
  • @harpo how do you handle a header row in your CsvReader class? i am using the methods above , verbatim, but the first row coming in is an array of the headers in the CSV. The rest of my data is of varying c# types, so that firt row breaks my app. – tCoe Jul 10 '18 at 20:24
  • Can someone explain why the character class `[^""]` includes two double quotes? A character class/negated character class consumes 1 character. Specifying the same character twice within the brackets is redundant right? – Minh Tran Jan 31 '19 at 21:41
  • 1
    @MinhTran - `""` is used to escape a double-quote within a C# verbatim string. – Mike Christiansen Jun 25 '20 at 19:29
76

The CSV format uses commas to separate values, values which contain carriage returns, linefeeds, commas, or double quotes are surrounded by double-quotes. Values that contain double quotes are quoted and each literal quote is escaped by an immediately preceding quote: For example, the 3 values:

test
list, of, items
"go" he said

would be encoded as:

test
"list, of, items"
"""go"" he said"

Any field can be quoted but only fields that contain commas, CR/NL, or quotes must be quoted.

There is no real standard for the CSV format, but almost all applications follow the conventions documented here. The RFC that was mentioned elsewhere is not a standard for CSV, it is an RFC for using CSV within MIME and contains some unconventional and unnecessary limitations that make it useless outside of MIME.

A gotcha that many CSV modules I have seen don't accommodate is the fact that multiple lines can be encoded in a single field which means you can't assume that each line is a separate record, you either need to not allow newlines in your data or be prepared to handle this.

reechard
  • 853
  • 6
  • 20
Robert Gamble
  • 97,930
  • 23
  • 141
  • 137
40

Put double quotes around strings. That is generally what Excel does.

Ala Eli,

you escape a double quote as two double quotes. E.g. "test1","foo""bar","test2"

Joe Phillips
  • 44,686
  • 25
  • 93
  • 148
11

You can put double quotes around the fields. I don't like this approach, as it adds another special character (the double quote). Just define an escape character (usually backslash) and use it wherever you need to escape something:

data,more data,more data\, even,yet more

You don't have to try to match quotes, and you have fewer exceptions to parse. This simplifies your code, too.

Adam Jaskiewicz
  • 10,728
  • 3
  • 32
  • 36
8

There is a library available through nuget for dealing with pretty much any well formed CSV (.net) - CsvHelper

Example to map to a class:

var csv = new CsvReader( textReader );
var records = csv.GetRecords<MyClass>();

Example to read individual fields:

var csv = new CsvReader( textReader );
while( csv.Read() )
{
    var intField = csv.GetField<int>( 0 );
    var stringField = csv.GetField<string>( 1 );
    var boolField = csv.GetField<bool>( "HeaderName" );
}

Letting the client drive the file format:
, is the standard field delimiter, " is the standard value used to escape fields that contain a delimiter, quote, or line ending.

To use (for example) # for fields and ' for escaping:

var csv = new CsvReader( textReader );
csv.Configuration.Delimiter = "#";
csv.Configuration.Quote = ''';
// read the file however meets your needs

More Documentation

NikolaiDante
  • 17,673
  • 14
  • 75
  • 112
5

In case you're on a *nix-system, have access to sed and there can be one or more unwanted commas only in a specific field of your CSV, you can use the following one-liner in order to enclose them in " as RFC4180 Section 2 proposes:

sed -r 's/([^,]*,[^,]*,[^,]*,)(.*)(,.*,.*)/\1"\2"\3/' inputfile

Depending on which field the unwanted comma(s) may be in you have to alter/extend the capturing groups of the regex (and the substitution).
The example above will enclose the fourth field (out of six) in quotation marks.

enter image description here

In combination with the --in-place-option you can apply these changes directly to the file.

In order to "build" the right regex, there's a simple principle to follow:

  1. For every field in your CSV that comes before the field with the unwanted comma(s) you write one [^,]*, and put them all together in a capturing group.
  2. For the field that contains the unwanted comma(s) you write (.*).
  3. For every field after the field with the unwanted comma(s) you write one ,.* and put them all together in a capturing group.

Here is a short overview of different possible regexes/substitutions depending on the specific field. If not given, the substitution is \1"\2"\3.

([^,]*)(,.*)                     #first field, regex
"\1"\2                           #first field, substitution

(.*,)([^,]*)                     #last field, regex
\1"\2"                           #last field, substitution


([^,]*,)(.*)(,.*,.*,.*)          #second field (out of five fields)
([^,]*,[^,]*,)(.*)(,.*)          #third field (out of four fields)
([^,]*,[^,]*,[^,]*,)(.*)(,.*,.*) #fourth field (out of six fields)

If you want to remove the unwanted comma(s) with sed instead of enclosing them with quotation marks refer to this answer.

Community
  • 1
  • 1
KeyNone
  • 7,527
  • 4
  • 32
  • 49
5

As mentioned in my comment to harpo's answer, his solution is good and works in most cases, however in some scenarios when commas as directly adjacent to each other it fails to split on the commas.

This is because of the Regex string behaving unexpectedly as a vertabim string. In order to get this behave correct, all " characters in the regex string need to be escaped manually without using the vertabim escape.

Ie. The regex should be this using manual escapes:

",(?=(?:[^\"\"]*\"\"[^\"\"]*\"\")*(?![^\"\"]*\"\"))"

which translates into ",(?=(?:[^""]*""[^""]*"")*(?![^""]*""))"

When using a vertabim string @",(?=(?:[^""]*""[^""]*"")*(?![^""]*""))" it behaves as the following as you can see if you debug the regex:

",(?=(?:[^"]*"[^"]*")*(?![^"]*"))"

So in summary, I recommend harpo's solution, but watch out for this little gotcha!

I've included into the CsvReader a little optional failsafe to notify you if this error occurs (if you have a pre-known number of columns):

if (_expectedDataLength > 0 && values.Length != _expectedDataLength) 
throw new DataLengthException(string.Format("Expected {0} columns when splitting csv, got {1}", _expectedDataLength, values.Length));

This can be injected via the constructor:

public CsvReader(string fileName, int expectedDataLength = 0) : this(new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
    _expectedDataLength = expectedDataLength;
}
MikeDub
  • 4,503
  • 2
  • 23
  • 42
  • How would you go about handling the Header row? i am trying to map the csv to C# objects that are all types, but the header row breaks it because its all strings... – tCoe Jul 10 '18 at 20:39
  • Isn't `[^""]` the same as `[^"]`? Duplication of a character inside a character class specification is redundant, right? – Minh Tran Jan 31 '19 at 21:58
4

Add a reference to the Microsoft.VisualBasic (yes, it says VisualBasic but it works in C# just as well - remember that at the end it is all just IL).

Use the Microsoft.VisualBasic.FileIO.TextFieldParser class to parse CSV file Here is the sample code:

 Dim parser As TextFieldParser = New TextFieldParser("C:\mar0112.csv")
 parser.TextFieldType = FieldType.Delimited
 parser.SetDelimiters(",")      

   While Not parser.EndOfData         
      'Processing row             
      Dim fields() As String = parser.ReadFields         
      For Each field As String In fields             
         'TODO: Process field                   

      Next      
      parser.Close()
   End While 
Mike Mackintosh
  • 13,156
  • 6
  • 54
  • 83
  • Yes, this is a very useful class in a somewhat unfortunate namespace ;-). To address the original question, however, you should also be setting `parser.HasFieldsEnclosedInQuotes = true;` and the input file would need to enclose fields that contain commas in quotes as per the CSV spec -- excel does this already. – Christopher King Sep 24 '14 at 14:28
4

You can use alternative "delimiters" like ";" or "|" but simplest might just be quoting which is supported by most (decent) CSV libraries and most decent spreadsheets.

For more on CSV delimiters and a spec for a standard format for describing delimiters and quoting see this webpage

Rufus Pollock
  • 2,155
  • 18
  • 19
3

If you feel like reinventing the wheel, the following may work for you:

public static IEnumerable<string> SplitCSV(string line)
{
    var s = new StringBuilder();
    bool escaped = false, inQuotes = false;
    foreach (char c in line)
    {
        if (c == ',' && !inQuotes)
        {
            yield return s.ToString();
            s.Clear();
        }
        else if (c == '\\' && !escaped)
        {
            escaped = true;
        }
        else if (c == '"' && !escaped)
        {
            inQuotes = !inQuotes;
        }
        else
        {
            escaped = false;
            s.Append(c);
        }
    }
    yield return s.ToString();
}
Neil
  • 3,511
  • 1
  • 24
  • 22
3

In Europe we have this problem must earlier than this question. In Europe we use all a comma for a decimal point. See this numbers below:

| American      | Europe        |
| ------------- | ------------- |
| 0.5           | 0,5           |
| 3.14159265359 | 3,14159265359 |
| 17.54         | 17,54         |
| 175,186.15    | 175.186,15    |

So it isn't possible to use the comma separator for CSV files. Because of that reason, the CSV files in Europe are separated by a semicolon (;).

Programs like Microsoft Excel can read files with a semicolon and it's possible to switch from separator. You could even use a tab (\t) as separator. See this answer from Supper User.

H. Pauwelyn
  • 11,346
  • 26
  • 71
  • 115
2

If you're interested in a more educational exercise on how to parse files in general (using CSV as an example), you may check out this article by Julian Bucknall. I like the article because it breaks things down into much smaller problems that are much less insurmountable. You first create a grammar, and once you have a good grammar, it's a relatively easy and methodical process to convert the grammar into code.

The article uses C# and has a link at the bottom to download the code.

Phil
  • 6,085
  • 2
  • 39
  • 64
2

Here's a neat little workaround:

You can use a Greek Lower Numeral Sign instead (U+0375)

It looks like this ͵

Using this method saves you a lot of resources too...

1

Just use SoftCircuits.CsvParser on NuGet. It will handle all those details for you and efficiently handles very large files. And, if needed, it can even import/export objects by mapping columns to object properties. In addition, my testing showed it averages nearly 4 times faster than the popular CsvHelper.

Jonathan Wood
  • 59,750
  • 65
  • 229
  • 380
0

You can read the csv file like this.

this makes use of splits and takes care of spaces.

ArrayList List = new ArrayList();
static ServerSocket Server;
static Socket socket;
static ArrayList<Object> list = new ArrayList<Object>();


public static void ReadFromXcel() throws FileNotFoundException
{   
    File f = new File("Book.csv");
    Scanner in = new Scanner(f);
    int count  =0;
    String[] date;
    String[] name;
    String[] Temp = new String[10];
    String[] Temp2 = new String[10];
    String[] numbers;
    ArrayList<String[]> List = new ArrayList<String[]>();
    HashMap m = new HashMap();

         in.nextLine();
         date = in.nextLine().split(",");
         name = in.nextLine().split(",");
         numbers = in.nextLine().split(",");
         while(in.hasNext())
         {
             String[] one = in.nextLine().split(",");
             List.add(one);
         }
         int xount = 0;
         //Making sure the lines don't start with a blank
         for(int y = 0; y<= date.length-1; y++)
         {
             if(!date[y].equals(""))
             {   
                 Temp[xount] = date[y];
                 Temp2[xount] = name[y];
                 xount++;
             }
         }

         date = Temp;
         name =Temp2;
         int counter = 0;
         while(counter < List.size())
         {
             String[] list = List.get(counter);
             String sNo = list[0];
             String Surname = list[1];
             String Name = list[2];
             for(int x = 3; x < list.length; x++)
             {           
                 m.put(numbers[x], list[x]);
             }
            Object newOne = new newOne(sNo, Name, Surname, m, false);
             StudentList.add(s);
             System.out.println(s.sNo);
             counter++;
         }
Eric
  • 9
  • 1
0

First, let's ask ourselves, "Why do we feel the need to handle commas differently for CSV files?"

For me, the answer is, "Because when I export data into a CSV file, the commas in a field disappear and my field gets separated into multiple fields where the commas appear in the original data." (That it because the comma is the CSV field separator character.)

Depending on your situation, semi colons may also be used as CSV field separators.

Given my requirements, I can use a character, e.g., single low-9 quotation mark, that looks like a comma.

So, here's how you can do it in Go:

// Replace special CSV characters with single low-9 quotation mark
func Scrub(a interface{}) string {
    s := fmt.Sprint(a)
    s = strings.Replace(s, ",", "‚", -1)
    s = strings.Replace(s, ";", "‚", -1)
    return s
}

The second comma looking character in the Replace function is decimal 8218.

Be aware that if you have clients that may have ascii-only text readers that this decima 8218 character will not look like a comma. If this is your case, then I'd recommend surrounding the field with the comma (or semicolon) with double quotes per RFC 4128: https://tools.ietf.org/html/rfc4180

l3x
  • 27,652
  • 1
  • 45
  • 35
0

I generally URL-encode the fields which can have any commas or any special chars. And then decode it when it is being used/displayed in any visual medium.

(commas becomes %2C)

Every language should have methods to URL-encode and decode strings.

e.g., in java

URLEncoder.encode(myString,"UTF-8"); //to encode
URLDecoder.decode(myEncodedstring, "UTF-8"); //to decode

I know this is a very general solution and it might not be ideal for situation where user wants to view content of csv file, manually.

hariszhr
  • 387
  • 1
  • 14
0

I usually do this in my CSV files parsing routines. Assume that 'line' variable is one line within a CSV file and all of the columns' values are enclosed in double quotes. After the below two lines execute, you will get CSV columns in the 'values' collection.

// The below two lines will split the columns as well as trim the DBOULE QUOTES around values but NOT within them
    string trimmedLine = line.Trim(new char[] { '\"' });
    List<string> values = trimmedLine.Split(new string[] { "\",\"" }, StringSplitOptions.None).ToList();
user1451111
  • 1,385
  • 2
  • 13
  • 25
  • 1
    Why my code is never displayed in multiple colors on StackOverflow? I indent by four spaces. – user1451111 Feb 10 '16 at 13:22
  • FYI... [There is no programming language tag on the question, so the highlighter doesn't know what language to highlight as.](https://meta.stackoverflow.com/questions/323748/why-is-c-syntax-highlighting-not-working-sometimes) – Nyerguds Apr 23 '18 at 07:40
0

The simplest solution I've found is the one LibreOffice uses:

  1. Replace all literal " by
  2. Put double quotes around your string

You can also use the one that Excel uses:

  1. Replace all literal " by ""
  2. Put double quotes around your string

Notice other people recommended to do only step 2 above, but that doesn't work with lines where a " is followed by a ,, like in a CSV where you want to have a single column with the string hello",world, as the CSV would read:

"hello",world"

Which is interpreted as a row with two columns: hello and world"

Daniel
  • 17,154
  • 10
  • 55
  • 72
  • 1
    By standard rules, any field containing either the split character or the quote is surrounded by quotes, and any quotes inside that are doubled, so there is no issue. Your `hello",world` field would simply need to be saved as `"hello"",world"`, which can be parsed 100% correctly. – Nyerguds Apr 17 '18 at 07:20
0
    public static IEnumerable<string> LineSplitter(this string line, char 
         separator, char skip = '"')
    {
        var fieldStart = 0;
        for (var i = 0; i < line.Length; i++)
        {
            if (line[i] == separator)
            {
                yield return line.Substring(fieldStart, i - fieldStart);
                fieldStart = i + 1;
            }
            else if (i == line.Length - 1)
            {
                yield return line.Substring(fieldStart, i - fieldStart + 1);
                fieldStart = i + 1;
            }

            if (line[i] == '"')
                for (i++; i < line.Length && line[i] != skip; i++) { }
        }

        if (line[line.Length - 1] == separator)
        {
            yield return string.Empty;
        }
    }
Rajat26
  • 31
  • 3
0

I used Csvreader library but by using that I got data by exploding from comma(,) in column value.

So If you want to insert CSV file data which contains comma(,) in most of the columns values, you can use below function. Author link => https://gist.github.com/jaywilliams/385876

function csv_to_array($filename='', $delimiter=',')
{
    if(!file_exists($filename) || !is_readable($filename))
        return FALSE;

    $header = NULL;
    $data = array();
    if (($handle = fopen($filename, 'r')) !== FALSE)
    {
        while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
        {
            if(!$header)
                $header = $row;
            else
                $data[] = array_combine($header, $row);
        }
        fclose($handle);
    }
    return $data;
}
Vi_real
  • 978
  • 1
  • 14
  • 25
0

I used papaParse library to have the CSV file parsed and have the key-value pairs(key/header/first row of CSV file-value).

here is example that I use:

https://codesandbox.io/embed/llqmrp96pm

it has dummy.csv file in there to have the CSV parsing demo.

I've used it within reactJS though it is easy and simple to replicate in app written with any language.

parag patel
  • 1,774
  • 1
  • 6
  • 20
0

An example might help to show how commas can be displayed in a .csv file. Create a simple text file as follows:

Save this text file as a text file with suffix ".csv" and open it with Excel 2000 from Windows 10.

aa,bb,cc,d;d "In the spreadsheet presentation, the below line should look like the above line except the below shows a displayed comma instead of a semicolon between the d's." aa,bb,cc,"d,d", This works even in Excel

aa,bb,cc,"d,d", This works even in Excel 2000 aa,bb,cc,"d ,d", This works even in Excel 2000 aa,bb,cc,"d , d", This works even in Excel 2000

aa,bb,cc, " d,d", This fails in Excel 2000 due to the space belore the 1st quote aa,bb,cc, " d ,d", This fails in Excel 2000 due to the space belore the 1st quote aa,bb,cc, " d , d", This fails in Excel 2000 due to the space belore the 1st quote

aa,bb,cc,"d,d " , This works even in Excel 2000 even with spaces before and after the 2nd quote. aa,bb,cc,"d ,d " , This works even in Excel 2000 even with spaces before and after the 2nd quote. aa,bb,cc,"d , d " , This works even in Excel 2000 even with spaces before and after the 2nd quote.

Rule: If you want to display a comma in a a cell (field) of a .csv file: "Start and end the field with a double quotes, but avoid white space before the 1st quote"

virgban
  • 49
  • 7
0

As this is about general practices let's start from rules of the thumb:

  1. Don't use CSV, use XML with a library to read & write the xml file instead.

  2. If you must use CSV. Do it properly and use a free library to parse and store the CSV files.

To justify 1), most CSV parsers aren't encoding aware so if you aren't dealing with US-ASCII you are asking for troubles. For example excel 2002 is storing the CSV in local encoding without any note about the encoding. The CSV standard isn't widely adopted :(. On the other hand xml standard is well adopted and it handles encodings pretty well.

To justify 2), There is tons of csv parsers around for almost all language so there is no need to reinvent the wheel even if the solutions looks pretty simple.

To name few:

  • for python use build in csv module

  • for perl check CPAN and Text::CSV

  • for php use build in fgetcsv/fputcsv functions

  • for java check SuperCVS library

Really there is no need to implement this by hand if you aren't going to parse it on embedded device.

Piotr Czapla
  • 23,150
  • 23
  • 90
  • 120
  • 12
    XML isn't always the answer. CSV is the right format for the job when you have a lot of dense, tabular data (i.e. a spreadsheet). Those tags introduce a lot of overhead, and if each and every line has an identical format, there is no need to be explicit about what each and every value represents. XML is great when you have complicated hierarchical data, or records with optional fields. That isn't always the case. – Adam Jaskiewicz Apr 20 '09 at 20:00
  • In theory the "tags" introduce a bit of overhead but I can't think of any real life application where it start to be a problem. Do you have any practical examples? To work on data one should use an database instead of csv. if we speak about data serialization (backups, data interchange), will it matter if parsing takes a week instead of 5 days? – Piotr Czapla Apr 20 '09 at 21:48
  • 2
    Basically, any situation in which you have data that is best represented by a table. Say you have data from a dozen different sensors that you sample every so often, and you record the timestamp and the value of each of the sensors at that time. Each record is identical: timestamp, sensor0, sensor1, ... sensor11. XML is great for representing complex, irregular data, but it is a rather heavyweight format that does not fit every single situation. KISS – Adam Jaskiewicz Apr 20 '09 at 22:10
  • I totally agree that xml is not an answer for everything. Especially It is not well suited as a database replacement nor for configuration files. But here the question was about data interchange for which XML was designed for. – Piotr Czapla Apr 21 '09 at 06:55
  • It depends what the data is, more than what it is used for. I don't know what data is being transferred, so I'm not sure if CSV is the most appropriate, but if it really is regular, tabular data, a tabular format simply makes more sense than a hierarchical format. Quite often people shoehorn data that *should* be XML into another format, but I think the opposite is just as bad, and it's a pet peeve of mine. – Adam Jaskiewicz Apr 21 '09 at 11:56
-1

I think the easiest solution to this problem is to have the customer to open the csv in excel, and then ctrl + r to replace all comma with whatever identifier you want. This is very easy for the customer and require only one change in your code to read the delimiter of your choice.

jamesdeath123
  • 3,196
  • 8
  • 39
  • 80
  • 1
    Who says they have Excel? In fact who says its even a human being who is doing the uploading?... – bytedev Sep 20 '16 at 15:47
-4

Use a tab character (\t) to separate the fields.

Pierre
  • 3,470
  • 1
  • 32
  • 33
  • 4
    -1 Great until someone uses a tab in their value then your right back to the problem the person asking the question has got. Swaping one delimiter char for another isnt going to solve the problem. – bytedev Sep 20 '16 at 15:45
  • Nonsense. People can't enter tabs in their data input. In most forms, that simply moves the data entry point to the next field. – Pierre Sep 21 '16 at 21:47
  • 7
    "People can't enter tabs in their data input"....are you serious?? A) of course a person could put a tab in a input field B) who says it is a GUI the data comes from? C) who says its even a human that is entering the data? – bytedev Sep 22 '16 at 13:20