0

My question is about CSV file in C#. If a field contains a comma how can this be exported into a CSV file as a one field? See Field 2 in the sample below:

Field1|Field2|Field3

10.20|Doe,John|Box No 1

I have this code:

//Get indices
indices = DenormalizeIndices(id);
foreach (DataRow row in indices.Tables[0].Rows)
{
 line = null;
  //We're only going to use selected fields
 for (i = 0; i < 3; i++)
 {
  if (line != null)
    line += DELIMITER;
  //Figure out the mapped field name we have defined
  if (i >= INDICES_TO_INCLUDE.Length)
    line += ""; //Nothing to populate
  else
  if (String.IsNullOrEmpty(INDICES_TO_INCLUDE[i]))
    line += "";
  else
    line += "=\"" + row[INDICES_TO_INCLUDE[i]].ToString() + "\"";

But with this code above the CSV file result will be as below, it will separate the index value of Field2:

Field1 | Field2 | Field3 | Field4

10.20 | Doe | John | Box No 1

What else should I add to the code so the result will be as below on Field2?

Field2

Doe, John

3
  • CSV stands for "Comma Separated Values". You can use whatever your want, but using a delimeter of | will not give you a CSV file. Commented Oct 7, 2013 at 20:09
  • I know what means by csv I just use pipes just to illustrate field separation. This is how it looks like in a text file with csv ="10.20", ="Doe,John",="Box No 1" @ BartoszKP Commented Oct 7, 2013 at 20:14
  • that is not CSV. CSV files do not have = signs before each field Commented Oct 7, 2013 at 22:59

3 Answers 3

3

CSV files that have text should wrap the text fields in double-quotes

1,2,"Hello world", "Dear, John"

As to how that relates to your code, I have no idea. Your line calculation has nothing to do with CSV.

Sign up to request clarification or add additional context in comments.

3 Comments

line += "=\"" + row[INDICES_TO_INCLUDE[i]].ToString() + "\""; what should I do with this code so that it will read the comma inside of field2 without separating when it is export to a CSV file
the code works well it was just when there is a comma the the field like in field2, the code is use to how many index field to output in the file. Example the name of my field are Field1 Field2 Field3 that is why this code is here { line = null; //We're only going to use selected fields for (i = 0; i < 3; i++)
line += "=\"" + row[INDICES_TO_INCLUDE[i]].ToString() + "\""; this code is how will it read the index value in each row.
1

Check out this document:

RFC 4180 - Common Format and MIME Type for Comma-Separated Values (CSV) Files

RFCs (Requests For Comment) are not standards, but this is as close to a CSV standard as you will find as is what most apps follow.

Basic rule is if your value contains a comma, newline, or double-quote, then surround it by double-quotes. Double-quotes in the values are escaped as two double-quotes.

Comments

0

If you really want each item to start with =, it should be inside your quotes, not outside. So line += "\"=" + row[INDICES_TO_INCLUDE[i]].ToString() + "\"";

To produce content like "=10.20", "=Doe,John","=Box No 1"

This should produce properly quoted CSV.

Though you also might just want to get rid of the =, I'm not sure why it's there in the first place.

2 Comments

if the out the = in the code line += "=\"" + row[INDICES_TO_INCLUDE[i]].ToString() + "\"";
Your comment makes no sense. But to reword, your code generates ="Doe,John", which results in two fields with quotes in them, ="Doe and John". In order to generate one quoted field, your code would need to generate "=Doe,John" (or just "Doe,John").

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.