【问题标题】:Read CSV with new line character inside cells在单元格内使用换行符读取 CSV
【发布时间】:2013-10-04 13:56:22
【问题描述】:

我正在尝试从 csv 中读取两列,行由 ';' 分隔。

我正在使用stram.ReadLine 方法,但问题是某些单元格的文本具有换行符,因此,ReadLine 方法将该单元格分成几个其他单元格,我该如何避免这种情况?为了简化这个模型,假设我有一列有 100 行,但其中一些里面有很长的文本和一些断行,我怎样才能将它修改为 100 行而不是更多?

StreamReader aFile = new StreamReader("C:\\dev\\csvReplacment\\szablonDE.csv");


            var dane = new List<string>();

            string line;

            while ((line = aFile.ReadLine()) != null)
            {
                dane.Add(line);
            }
            aFile.Close();

【问题讨论】:

  • 你试过walgreens吗
  • 沃尔格林?那是什么?
  • 你能提供一个内容的例子吗?此外,如果有新行,您可能不想ReadLine,而是阅读直到下一次出现; 或文件末尾(以先到者为准)。
  • 我认为在你的标题中你的意思是 CSV 就是全部。你能发布一个示例 CSV
  • 如何使用 \r 和 \n 读取 csv 的内容?

标签: c# csv


【解决方案1】:

假设; 标志着一行的结束:

    // Build your final resulting list
    List<String> dane = new List<String>();

    // use StreamReader to read the file
    using (StreamReader sr = new StreamReader(ms))
    {
        // create a string builder that we can use to store each
        // line's contents until it's ready to be added to dane
        StringBuilder builder = new StringBuilder();
        // buffer char
        Char c;
        // read the stream character by character
        while (!sr.EndOfStream)
        {
            c = (Char)sr.Read();
            // if it's `;` it's the end of a row, so add it to
            // dane and reset the line's contents
            if (c == ';')
            {
                dane.Add(builder.ToString());
                builder.Clear();
            }
            // avoid reading in superfluous whitespace before we
            // begin reading a line
            else if (builder.Length == 0 && Char.IsWhiteSpace(c))
            {
                continue;
            }
            // concatenate the current character to our line
            else
            {
                builder.Append(c);
            }
        }
        // if there's a final row, add it to dane
        if (builder.Length > 0)
        {
            dane.Add(builder.ToString());
        }
    }

    // dane now contains each line's contents.

您可以优化它并一次读取 1024 个字符并在其中搜索 ;,但这只是一个简单的示例,向您展示如何开始。

【讨论】:

    【解决方案2】:

    使用来自 Nuget 的现有 CSV 解析器。 Nuget (http://www.nuget.org/packages/CsvTools/) 上的“CSVTools”可以处理这个问题,并且速度非常快,支持绑定到强 .NET 类型以便于解析。

    http://blogs.msdn.com/b/jmstall/archive/2012/03/24/opensource-csv-reader-on-nuget.aspx

    用法是这样的:

    var dt = DataAccess.DataTable.New.Read(@"c:\temp\test.csv"); 
    foreach (Row row in dt.Rows())  { } 
    

    【讨论】:

      【解决方案3】:

      我建议您简单地使用一些现有的代码/库,而不是自己去挤成一团

      http://csvfile.codeplex.com/ http://www.codeproject.com/Articles/12170/FileHelpers-v2-0-Delimited-CSV-or-Fixed-Data-Impor

      只有两个

      我还建议使用 ServiceStack.Text nuget https://www.nuget.org/packages/ServiceStack.Text/3.9.64

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-09
        • 2017-10-07
        相关资源
        最近更新 更多