hyliandanny
asked on
How do I parse quotation marks in C#?
I'm trying to read a CSV file that may have quotation marks in its values. This affects cases where the quotation marks denote a string, which could have commas in it.
Is this the correct way of detecting quotation marks in C#?
while (!lineValues[j].Contains(" \""))
That's throwing an out of index exception, so I want to make sure I'm correctly saying "until we find a field that has quotation marks in it...", especially since the file is formatted correctly.
Is this the correct way of detecting quotation marks in C#?
while (!lineValues[j].Contains("
That's throwing an out of index exception, so I want to make sure I'm correctly saying "until we find a field that has quotation marks in it...", especially since the file is formatted correctly.
while (!lineValues[j].Contains("\""))
This is probably information overload, but take a look at this. It will handle reading in the CSV with all the scenarios that you are facing. It's kind of nice to use some pre-written code to make your life easier.
http://www.codeproject.com/KB/database/CsvReader.aspx
http://www.codeproject.com/KB/database/CsvReader.aspx
ASKER
@ Triggerito:
I can see why you would say that. Here's the rest of the code. Perhaps I'm just tired and my logic is indeed faulty, but I want the while loop to go on until quotation marks are found because this loop only happens when there are more commas then there should be. Only when there are strings with commas in them (within quotation marks) will this loop be entered.
I can see why you would say that. Here's the rest of the code. Perhaps I'm just tired and my logic is indeed faulty, but I want the while loop to go on until quotation marks are found because this loop only happens when there are more commas then there should be. Only when there are strings with commas in them (within quotation marks) will this loop be entered.
using (StreamReader reader = new StreamReader(@"C:\sample.csv")) {
string buffer;
int violatingRow = 0;
// Iterate through all lines
while ((buffer = reader.ReadLine()) != null) {
string[] lineValues = buffer.Split(',');
// Size of the row array. Can't change the member, so use a local variable.
int trueCSVLength = lineValues.Length;
// It's possible that the number of strings in the "split" array is > n , where n is the
// number of column headers. We expect the rest of the comma-separated values to behave
// according to the guidelines; that is, there are (n - (this column's index)) values
// left to be read. Those strings with quotation marks tell us where a value truly begins
// and ends; combine them so indices continue to fall neatly as designated.
if (lineValues.Length > (int)EColumns.e_colCount) {
// First string is in Name index.
int i = (int) EColumns.e_colName;
try {
// Find the value with the first set of quotation marks.
while (!lineValues[i].Contains("\""))
i++;
// We've got our index. Now find the next set, but put together the single field
// along the way
int j = i;
while (!lineValues[j].Contains("\"")) {
lineValues[i].Insert(lineValues[i].Length, lineValues[++j]);
// Set the remaining fields back one index
for (int k = j; k < lineValues.Length; k++) {
lineValues.SetValue(lineValues[k + 1], k);
}
trueCSVLength--;
}
}
catch (Exception ex) {
// If there's any problems, we're not going to read the file anymore. Report. Bail.
csv_labelWarning.Text = ex.Message;
return;
}
}
...
ASKER
@ hehdaddy
I've seen that project; in addition to information overload (the Web namespace usually intermingles and is used for whatever I am trying to do), 2008 C# Express doesn't seem to convert it properly since it was created with an older version of Visual Studio.
I've seen that project; in addition to information overload (the Web namespace usually intermingles and is used for whatever I am trying to do), 2008 C# Express doesn't seem to convert it properly since it was created with an older version of Visual Studio.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you. It's probably my logic, then...
I'm a little confused. The question was not if the .Contains("\"") works, but why the code provided was returning an exception.
How did Ramone_Hamilton's answer solve the problem?
How did Ramone_Hamilton's answer solve the problem?
ASKER
Hi, Tiggerito.
I wanted to rule out the actual C#-specific element: the escape operator, backslash (\). This is because I'm a C# newbie; if I know that it's actually correctly detecting quotation marks, then my own logic is probably at fault, which I actually did not expect further assistance with.
The program is probably entering this case incorrectly; since the backslash operator is the correct way of detection, then the code is returning an exception because I must have done something foolish when entering this case.
If you're curious to actually help me further, a larger chunk of code will be necessary. That would be amazing, but it wasn't what I was expecting. Thanks for your concern!
I wanted to rule out the actual C#-specific element: the escape operator, backslash (\). This is because I'm a C# newbie; if I know that it's actually correctly detecting quotation marks, then my own logic is probably at fault, which I actually did not expect further assistance with.
The program is probably entering this case incorrectly; since the backslash operator is the correct way of detection, then the code is returning an exception because I must have done something foolish when entering this case.
If you're curious to actually help me further, a larger chunk of code will be necessary. That would be amazing, but it wasn't what I was expecting. Thanks for your concern!
Most likely your out of index exception is being thrown because you are inserting values into the array that you are looping through. You need to create a 2nd array to use for inserting, etc., so that your lineValues don't go out of index.
ASKER
I was about to put your theory to the test, hehdaddy, but the portion before I insert into lineValues is actually throwing the out of bounds exception (as well?). Since this is where I find the comma-containing strings I'll be joining together, I can't really make a secondary array to verify the behavior you're suggesting.
That is the code in lines 25-26 in the earlier post. I'll copy/paste for convenience.
Additionally, I'll supply my sample file I'm using when causing this exception to be thrown so we can figure this out.
That is the code in lines 25-26 in the earlier post. I'll copy/paste for convenience.
Additionally, I'll supply my sample file I'm using when causing this exception to be thrown so we can figure this out.
while (!lineValues[i].Contains("\""))
i++;
sample-txt-for-upload.txt
ASKER
Perhaps also relevant is the fact that when I trace through the program while debugging, what would be the quotation marks (") appear as squares, almost as if it does not recognize that character.
If it indeed is some exceptional character situation, then this would all make sense.
If it indeed is some exceptional character situation, then this would all make sense.
Your sample text contains two different types of double quotes which are not the type your code uses. Maybe this is related to the problem.
Quotes (||)
"
Open Quotes (\\)
Close Quotes (//)
Quotes (||)
"
Open Quotes (\\)
Close Quotes (//)
ASKER
Tiggerito, I think you're on to it. I posted a question which specifically addresses the patterns you've informed me about.
https://www.experts-exchange.com/questions/24308232/How-do-I-recognize-a-particular-type-of-quotation-mark-set-in-C.html
https://www.experts-exchange.com/questions/24308232/How-do-I-recognize-a-particular-type-of-quotation-mark-set-in-C.html
Open in new window