非常简单的C#CSV阅读器

DNN*_*DNN 37 c# csv

我想从CSV文件创建一个数组.

这很简单,你可以想象,CSV文件只有一行和这些值:

Device, SignalStrength, Location, Time, Age.
Run Code Online (Sandbox Code Playgroud)

我想把这些值放到一维数组中.

我已经尝试了一些例子,但它们都比需要的更复杂.

Ram*_*esh 64

您可以尝试下面的LINQ代码段.

string[] allLines = File.ReadAllLines(@"E:\Temp\data.csv");

    var query = from line in allLines
                let data = line.Split(',')
                select new
                {
                    Device = data[0],
                    SignalStrength = data[1],
                    Location = data[2], 
                    Time = data[3],
                    Age = Convert.ToInt16(data[4])
                };
Run Code Online (Sandbox Code Playgroud)

更新:在一段时间内,事情发生了变化.截至目前,我更愿意使用这个库http://www.aspnetperformance.com/post/LINQ-to-CSV-library.aspx

  • @ramesh"有些人在遇到问题时,会想'我知道,我会使用正则表达式.' 现在他们有两个问题." - 杰米·扎温斯基 看看http://secretgeek.net/csv_trouble.asp. (14认同)
  • 嗨Ramesh ..你的解决方案很棒......但是你怎么逃避逗号? (7认同)

And*_*are 51

如果只有一行,那么做这样的事情:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        String[] values = File.ReadAllText(@"d:\test.csv").Split(',');
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 如果任何字段包含逗号,则会失败. (104认同)
  • 不,不,不,这太复杂了!! (73认同)
  • @jamesdeath123 CSV列值可以包含逗号,只要它们用双引号括起来,例如"this is,a test". (17认同)
  • 这个答案是错误的,不应该是接受的答案,因为除了最基本的csv文件之外它不会处理任何东西,并且大多数可以创建CSV的系统将能够创建该解决方案无法加载的文件.其中我正在寻找解决方案的文件类型. (11认同)
  • 你如何处理新线? (5认同)
  • 我认为这不是一个正确的答案,因为在某些情况下你可能会将逗号作为数据.例如,员工记录为CSV行"Mr.","Scott","","babble","","scotty @ hotmail.com","Ralph Diagnostics","","经理,CRM和数据分析" ,"分析"当我们解析这些数据时,经理,CRM和数据分析将成为两列,因为它只是一个列数据 (3认同)
  • 我同意大卫的观点.这不应该标记为正确的答案.试试这个:https://gist.github.com/mariodivece/9614872 (3认同)

Chr*_*son 8

这是我做的一个简单的功能.它接受字符串CSV行并返回字段数组:

它适用于Excel生成的CSV文件以及许多其他变体.

    public static string[] ParseCsvRow(string r)
    {

        string[] c;
        string t;
        List<string> resp = new List<string>();
        bool cont = false;
        string cs = "";

        c = r.Split(new char[] { ',' }, StringSplitOptions.None);

        foreach (string y in c)
        {
            string x = y;


            if (cont)
            {
                // End of field
                if (x.EndsWith("\""))
                {
                    cs += "," + x.Substring(0, x.Length - 1);
                    resp.Add(cs);
                    cs = "";
                    cont = false;
                    continue;

                }
                else
                {
                    // Field still not ended
                    cs += "," + x;
                    continue;
                }
            }

            // Fully encapsulated with no comma within
            if (x.StartsWith("\"") && x.EndsWith("\""))
            {
                if ((x.EndsWith("\"\"") && !x.EndsWith("\"\"\"")) && x != "\"\"")
                {
                    cont = true;
                    cs = x;
                    continue;
                }

                resp.Add(x.Substring(1, x.Length - 2));
                continue;
            }

            // Start of encapsulation but comma has split it into at least next field
            if (x.StartsWith("\"") && !x.EndsWith("\""))
            {
                cont = true;
                cs += x.Substring(1);
                continue;
            }

            // Non encapsulated complete field
            resp.Add(x);

        }

        return resp.ToArray();

    }
Run Code Online (Sandbox Code Playgroud)