How do I remove the quotation marks from a .csv file?

10,315

Solution 1

One approach would be with Regex, consider this pattern:

\"

Regular expression visualization

Debuggex Demo

It will match all " in the string so you could do something like this:

var s = Regex.Replace(input, pattern, string.Empty);

Here input would be the entire file or even just one line, the pattern would be \", and s would be the resulting string after the removal of those double quotes.

Solution 2

It is much easier to use some existing CSVParser instead of doing this manually. You could use for example:

Parsing CSV files manually introduces a lot of potential risks. Even if you solve your above challenge you cannot be sure that there won't be other cases when your CSV file will not be parsed appropriately.

Solution 3

The reason why it's not working lies here:

List<string> listItems = listLine.Split(';').ToList();
foreach(String item in listItems)
{
    if (item == "&quot;") {
        item2 = item.Replace("&quot;", "");
    }
}

Think about what kind of data item is going to hold, because it seems like you are checking to see if a char is equal to &quot, which is the correct thing to do, but maybe item isn't a char? Think about what List<string> listItems = listLine.Split(';').ToList(); and foreach(String item in listItems are doing :)

Share:
10,315
crmepham
Author by

crmepham

I am currently coding in Java, Kotlin and Go.

Updated on July 14, 2022

Comments

  • crmepham
    crmepham almost 2 years

    I read in a .csv file.

    The contents of which looks like this:

    1;"final60";"United Kingdom";"2013-12-06 15:48:16";

    2;"donnyr8";"Netherlands";"2013-12-06 15:54:32"; etc

    At the moment I am just trying to remove the quotation marks from each line using the Replace method. This is what I have attempted which doesn't appear to do anything. Although doesn't seem to break the program in anyway.

    try
    {
        string item2;
        List<string> list = File.ReadLines("file.csv").ToList();
        foreach (string listLine in list)
        {
             Console.Write("#  ");
             // seperate up this line into a new list by ; 
             List<string> listItems = listLine.Split(';').ToList();
             foreach(String item in listItems)
             {
                  if (item == "&quot;")
                  {
                        item2 = item.Replace("&quot;", "");
                  }
                  else
                  {
                        item2 = item;
                  }
                  Console.Write(item2);
             }
             Console.WriteLine("\n");
        }
    }
    catch (Exception e)
    {
        // Let the user know what went wrong.
        Console.WriteLine("The file could not be read:");
        Console.WriteLine(e.Message);
    }
    

    How can I remove the quotation marks in each line?

  • crmepham
    crmepham over 10 years
    Thanks, although not entirely relevant.
  • crmepham
    crmepham over 10 years
    Thanks for the references, I am however very new to C# and trying to get a hold of the basics for now.
  • crmepham
    crmepham over 10 years
    I assumed listItems would be a string (which obviously is an array of characters). So I used the foreach loop to iterate through each character in the string to check if anyone of them was a quotation mark!?
  • Justin C
    Justin C about 10 years
    That is what you want to do, yes. But you are not iterating through each character in each string, you are looping through each string inside listItems. You'd want to see each character by itself, and right now you are comparing a whole string to a single character.