读取CSV文件包括带逗号的字符串
本文关键字:字符串 CSV 文件包 读取 | 更新日期: 2023-09-27 17:50:55
我有一个csv文件,其中有许多这种格式的字符串文件:例如"1,125"当我读取Csv时,这些字符串是空的。
string header = isFirstRowHeader ? "Yes" : "No";
string pathOnly = Path.GetDirectoryName(path);
string fileName = Path.GetFileName(path);
string sql = @"SELECT * FROM [" + fileName + "]";
using (OleDbConnection connection = new OleDbConnection(
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly +
";Extended Properties='"Text;HDR=" + header + "'""))
using (OleDbCommand command = new OleDbCommand(sql, connection))
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
DataTable dataTable = new DataTable();
dataTable.Locale = CultureInfo.CurrentCulture;
adapter.Fill(dataTable);
}
我的CSV文件是这样的:
1392/12/12,501
ABS、15日,"1205",KTI1212,"123 5",KK
检查此代码:
try
{
//Read the file
System.IO.StreamReader f = new System.IO.StreamReader(fileName, System.Text.Encoding.UTF8);
string line = f.ReadLine();
//Split text by comma
string[] tokens = line.Split(',');
int tokenCount = tokens.Length;
//Reconstruct the broken strings - tokensList retreives the final values
System.Collections.ArrayList tokensList = new System.Collections.ArrayList();
string token = "";
bool stringFound = false;
string currentToken;
for (int i=0; i<tokenCount; i++)
{
currentToken = tokens[i];
if (!stringFound)
{
//Check if not a string
if ((currentToken.Trim().StartsWith("'"") && !currentToken.Trim().EndsWith("'"")) || currentToken.Trim().Equals("'""))
{
stringFound = true;
token = currentToken;
}
else
{
//Add the string as final value
tokensList.Add(currentToken);
}
}
else
{
//Reconstruct the string
token += "," + currentToken;
if (currentToken.Trim().EndsWith("'""))
{
int quoteIndex = currentToken.LastIndexOf("'"");
if (quoteIndex == 0 || (quoteIndex > 0 && currentToken[quoteIndex-1] != '"'))
{
tokensList.Add(token);
stringFound = false;
token = "";
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}