【问题标题】:Escape comma(,) from a csv cell while while exporting its data to database table在将数据导出到数据库表时,从 csv 单元格中转义逗号(,)
【发布时间】:2020-03-17 04:37:39
【问题描述】:

我有一个 csv 文件,其中有一个包含逗号的字段。例如,在办公室位置列下,我有一个值 xyz,建筑。当我通过调试器检查值时,它只显示“\”xyz”。我尝试使用Replace(",","")Replace("\"","") 转义逗号和反斜杠,但它失败了。另外我在结果中得到了额外的\用红色圆圈标记。

我在调试显示 csv 行的结构时附上了图像。问题出在红色圆圈区域。

我也试过以下功能:

public static string RemoveColumnDelimitersInsideValues(string input)
    {

        const char valueDelimiter = '"';
        const char columnDelimiter = ',';

        StringBuilder output = new StringBuilder();

        bool isInsideValue = false;
        for (var i = 0; i < input.Length; i++)
        {
            var currentChar = input[i];

            if (currentChar == valueDelimiter)
            {
                isInsideValue = !isInsideValue;
                output.Append(currentChar);
                continue;
            }

            if (currentChar != columnDelimiter || !isInsideValue)
            {
                output.Append(currentChar);
            }

        }
        return output.ToString();
    }

请帮助解决问题。谢谢

【问题讨论】:

  • 实际上,在我看来,它只是在调试器视图中存在这个反斜杠。您应该尝试将此字符串输出到文件以查看反斜杠是否真的在这里
  • 最简单的方法是使用现成的 CSV 解析器,该解析器还将处理值内的 ",并将其转义为 ""
  • 如果您的数据有逗号,那么您应该以逗号分隔值(csv)的不同格式保存。尝试使用制表符分隔值(tsv)。

标签: c# .net csv datatable delimiter


【解决方案1】:

您看到的 \ 字符不在实际字符串中,它只是在调试器视图中添加的转义字符。

点击放大镜可以得到字符串的实际值。

希望对你有帮助。

【讨论】:

    【解决方案2】:

    尝试使用 TextFieldParser,在 csv 中,如果列值有逗号,则列值用 qoutes 转义,因此将 HasFieldsEnclosedInQuotes 添加为 true 将自动将其读取为单列。

    using Microsoft.VisualBasic.FileIO;
    
    
    using (TextFieldParser reader = new TextFieldParser(csvpath))
           {
             reader.Delimiters = new string[] { "," };
             reader.HasFieldsEnclosedInQuotes = true;
             string[] col =  reader.ReadFields();
           }
    

    【讨论】:

      【解决方案3】:

      String.Replace 不会修改现有字符串,它会返回一个新字符串。因此,您在 IsNullOrEmpty 检查之外有相同的旧 row 字符串。

      另外,您说的是,您正在尝试转义逗号和引号,但您正在代码中删除它。

      如果您想删除逗号和引号,您的代码可能如下所示

      if (string.IsNullOrEmpty(row))
      {
          row = row.Replace(",", "").Replace("\"", "");
      }
      

      如果你想转义引号和逗号,你的代码可能看起来像

      if (row != null && row.Contains(","))
      {
          row = "\"" + row.Replace("\"", "\"\"") + "\"";
      }
      

      【讨论】:

        【解决方案4】:

        您的代码有 3 个问题值得指出。

        1。解析 CSV 可能很棘手

        你能正确处理多行字符串吗?你会在其中一列中编码处理"(所以转义")吗?

        我建议使用 csv 读取库(又名 NuGet 包)。

        没有反斜杠

        这是一个文件。

        1,"The string in the first row has a comma, and an f, in it"
        2,The string in the 2nd row does not have a comma in it 
        

        这是 Visual Studio 显示的内容(我在这里使用 VS Code)。

        这是 Console.WriteLine 打印的内容。

        1,"The string in the first row has a comma, and an f, in it"
        2,The string in the 2nd row does not have a comma in it 
        

        3。替换逗号

        即使你处理引号,替换逗号不会摆脱字段分隔符吗?

        【讨论】:

          猜你喜欢
          • 2012-08-05
          • 2023-03-27
          • 2014-08-02
          • 1970-01-01
          • 2013-06-17
          • 1970-01-01
          • 2015-10-18
          • 2023-01-16
          • 2012-12-22
          相关资源
          最近更新 更多