CSV row split into string array question
How would you split this row to a string array?
the problem is Rutois, a.s. , so you cannot directly split with ','
separator..
543472,"36743721","Rutois, a.s.","151","some name","01341",55开发者_开发百科,"112",1
thanks
I would recommend you using a CSV parser instead of rolling your own.
FileHelpers is a nice library for this job.
You can use a regular expression to pick out the values from the line:
string line ="543472,\"36743721\",\"Rutois, a.s.\",\"151\",\"some name\",\"01341\",55,\"112\",1";
var values = Regex.Matches(line, "(?:\"(?<m>[^\"]*)\")|(?<m>[^,]+)");
foreach (Match value in values) {
Console.WriteLine(value.Groups["m"].Value);
}
Output:
543472
36743721
Rutois, a.s.
151
some name
01341
55
112
1
This of course assumes that you actually have got the complete CSV record in the string. Note that values in a CSV record can contain line breaks, so getting the records from a CSV file can not be done by simply splitting it on line breaks.
you can connect to file using odbc check this
link (If link does not help much just google it "connecting csv files with odbc")
If you have problems in odbc also i guess the file is not a valid csv file.
I'd be tempted to swap out the quotes that occur inside the quoted strings and then use split. this would work.
string csv = "543472,\"36743721\",\"Rutois, a.s.\",\"151\",\"some name\",\"01341\",55,\"112\",1";
const string COMMA_TOKEN = "[COMMA]";
string[] values;
bool inQuotes = false;
StringBuilder cleanedCsv = new StringBuilder();
foreach (char c in csv)
{
if (c == '\"')
inQuotes = !inQuotes; //work out if inside a quoted string or not
else
{
//Replace commas in quotes with a token
if (inQuotes && c == ',')
cleanedCsv.Append(COMMA_TOKEN);
else
cleanedCsv.Append(c);
}
}
values = cleanedCsv.ToString().Split(',');
//Put the commas back
for (int i = 0; i < values.Length; i++)
values[i] = values[i].Replace(COMMA_TOKEN, ",");
I'm guess you want something like this -
string csv = 543472,"36743721","Rutois, a.s.","151","some name","01341",55,"112",1 ;
string[] values;
values = csv.Split(",");
for(int i = 0; i<values.Length; i++)
{
values[i] = values[i].Replace("\"", "");
}
Hope this helps.
The other RegEx answer will fail if the first character is a quote.
This is the correct regular expression:
string[] columns = Regex.Split(inputRow, ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
精彩评论