【问题标题】:Read csv multi column & export it读取 csv 多列并导出
【发布时间】:2016-08-30 02:20:58
【问题描述】:

我的 csv 数据如下所示:

Device data for period 30/08/2016 to 30/08/2016
Site ID,Time,INC1_MD
VSI-18,2016-08-30 00:00:00,165.954
VSI-18,2016-08-30 00:01:00,14.524
VSI-18,2016-08-30 00:02:00,32.920
VSI-18,2016-08-30 00:03:00,48.508
VSI-18,2016-08-30 00:04:00,62.418

.....

我尝试忽略前两行并从“VSI-18...”开始 并提取日期和时间列之后的第三列数据 并将它们导出到新的 csv 文件中,每天 1 列 喜欢:

day1,day2,day3
100,200,300
200,123,123
123,222,444
....

这是我的代码

        o_csv_loc.Text = varFile; //csv data file location
        save_file_loc.Text = saveloc; //new csv file location

        var reader = new StreamReader(File.OpenRead(varFile));
        List <string[]> listA = new List<string[]>();
        List<string[]> listB = new List<string[]>();
        List<string[]> listC = new List<string[]>();

        //I think these two code below is to skip first 2 line of csv data                
        //file and start read the third line (VSI-18...)
        reader.ReadLine();
        reader.ReadLine();

        while (reader.Peek() > -1)
        {
            var line = reader.ReadLine();
            var values = line.Split(';');

            listA.Add(new string[] { values[0] });
            listB.Add(new string[] { values[1] });
            listC.Add(new string[] { values[2] });
            //I think that listC is suppose to extract the data after the 
            //second comma which is third column
        }

对于导出数据代码,我还没有完成,因为我还不知道如何读取数据。 调试时,'System.IndexOutOfRangeException' 在线显示

   listB.Add(new string[] { values[1] });

这条线应该没有问题吧? values[0] 还没有问题。

编辑

我成功将数据导出到新的 csv 文件

var reader = new StreamReader(File.OpenRead(varFile));
        List <string[]> listA = new List<string[]>(); //here are the code 
        //changed

        List<string[]> listB = new List<string[]>();
        List<string[]> listC = new List<string[]>();
        reader.ReadLine();
        reader.ReadLine();

        while (reader.Peek() > -1)
        {
            var line = reader.ReadLine();
            var values = line.Split(',');

            listA.Add(new string[] { values[0] });
            listB.Add(new string[] { values[1] });
            listC.Add(new string[] { values[2]});

        }

        using (System.IO.TextWriter writer = File.CreateText(saveloc))
        {
            for (int index = 0; index < listC.Count; index++)
            {
                writer.WriteLine(string.Join(",", listC[index]) + ',');
            }
        }

结果是这样的:

165.954,
14.524,
32.920,
48.508,
62.418,
79.151,
96.982,

我还在想如何检测新日期并放入新列

【问题讨论】:

  • 你在 ; 上分手。我在您的文件中没有看到任何这些内容。
  • 不要在 sn-p 标记中包含非 HTML/JS/CSS 代码。将其缩进四个空格就足够了(或突出显示它并单击{} 按钮)。
  • 正如@Blorgbeard 提到的,我认为你想分割','而不是';'
  • 当心自定义 CSV 解析器。这是一种比最初看起来更微妙的格式。
  • 我很抱歉我的代码有这么小的错误,是我的错,现在读取的代码没问题,现在只需要将数据导出到新的 csv。

标签: c# csv


【解决方案1】:

首先,就像其中一个 cmets 所述...在您的代码中,您使用 ; 作为分隔符,但是 csv 文件使用 ,...所以var values = line.Split(';'); 的结果是一个数组只有一个元素。

其次,我会保护我的应用程序免受错误格式或损坏数据的影响。例如

var line = reader.ReadLine();

if(string.IsNullOrEmpty())//<--empty row
    continue;//<--ignore, or else add empty values to your in-memory lists

var values = line.Split(',');

listA.Add(new string[] { values.length > 0 ? values[0] : string.Empty });
listB.Add(new string[] { values.length > 1 ? values[1] : string.Empty });
listC.Add(new string[] { values.length > 2 ? values[2] : string.Empty });

//or simply

if(values.length < 3)
    continue;//<--ignore, or else add empty values to your in-memory lists

【讨论】:

    【解决方案2】:

    几分。

    由于使用的分隔符 (;) 实际上并未拆分字符串,因此您可能会收到错误/异常,因此 values[1] 会抛出 IndexOutOfRange 异常。使用正确的分隔符(, 你需要的)。

    如果您的意图是生成新的csv(具有相同的字符串),为什么需要拆分字符串?不能直接写入文件吗(假设是,定界)?

    var sb = new StringBuilder();
    while (reader.Peek() > -1)
    {
         var line = reader.ReadLine();
         sb.AppendLine(line);
    }
    
    // Write to file.
    File.WriteAllText(filePath, sb.ToString());
    

    【讨论】:

    • 我的意思是,我想读取 csv 数据文件(我有一个下载器可以自动下载新的 csv 文件),并且我想每天将其放入新列中。如您所见,示例数据是 8 月 30 日,而我想要的唯一数据是第二个逗号之后。我想将该数据提取到新的 csv 文件中。当开始新的一天时,它会在昨天的数据旁边导出一个新的一天数据,例如:第1天的数据,第2天的数据,第3天的数据
    • 这就是我真正的意思,从下载的 csv 文件中获取所需内容的子字符串(直到第一个逗号),然后根据需要附加任何新列,然后写入新文件。
    • 我知道你的意思,但我的数据是这样的(评论导致你误解,也许?)see here for details Day1 数据将并且需要在 Day2 数据进来之前先完成收集。我可以' 不等整月数据采集完毕再使用 .append 写入,它会先写入第 1 天的数据,当第 2 天到来时,将第 2 天的数据写入新列
    猜你喜欢
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 1970-01-01
    • 2021-05-09
    相关资源
    最近更新 更多