How do I remove the quotation marks from a .csv file?
Solution 1
One approach would be with Regex, consider this pattern:
\"
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 == """) {
item2 = item.Replace(""", "");
}
}
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 "
, 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 :)
Comments
-
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 == """) { item2 = item.Replace(""", ""); } 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 over 10 yearsThanks, although not entirely relevant.
-
crmepham over 10 yearsThanks for the references, I am however very new to C# and trying to get a hold of the basics for now.
-
crmepham over 10 yearsI assumed
listItems
would be a string (which obviously is an array of characters). So I used theforeach
loop to iterate through each character in the string to check if anyone of them was a quotation mark!? -
Justin C about 10 yearsThat 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.