简体   繁体   English

按日期多次删除csv行

[英]Multiple delete in csv rows by dates

To pursue my goal of deleting multiple lines in csv file. 追求我删除csv文件中多行的目标。 My idea is to get the dates between two dates that are not include for deletion. 我的想法是获取两个日期之间不包括删除的日期。 But how can I iterate days between two dates and get there values to string and used to my code for evaluating the csv file that contains this field. 但是我该如何迭代两个日期之间的几天,并在那里获取值以字符串化,并用于我的代码来评估包含该字段的csv文件。

  private void button2_Click(object sender, EventArgs e)
  {
    DateTime start = new DateTime(01 / 25 / 2015);
    DateTime end = new DateTime(01 / 27 / 2015);

    var dates = new List<DateTime>();

    for (var dt = start; dt <= end; dt = dt.AddDays(1))
    {
      dates.Add(dt);

      var oldLines = System.IO.File.ReadAllLines(txtFileName.Text);
      var newLines = oldLines.Where(line => 
      line.Contains(dates.ToString()));          
      System.IO.File.WriteAllLines(txtFileName.Text, newLines);

      newLines = oldLines.Select(line => new

        {
            Line = line,
            Words = line.Split(' ')
        })
         .Where(lineInfo => lineInfo.Words.Contains(dates.ToString()))
         .Select(lineInfo => lineInfo.Line);
     }
  } 

INPUT: 输入:

"EmployeeCode","Date","Time","Type" “雇员代码”,“日期”,“时间”,“类型”
"3434","01/22/2013","07:54","0" “ 3434”,“ 2013/01/22”,“ 07:54”,“ 0”
"3023","01/23/2014","07:54","0" “ 3023”,“ 2014/01/23”,“ 07:54”,“ 0”
"2897","01/24/2015","07:54","0" “ 2897”,“ 2015/01/24”,“ 07:54”,“ 0”
"3734","01/25/2015","07:54","0" “ 3734”,“ 2015年1月25日”,“ 07:54”,“ 0”
"3168","01/26/2015","07:54","0" “ 3168”,“ 2015年1月26日”,“ 07:54”,“ 0”
"4863","01/26/2015","07:55","0" “ 4863”,“ 2015年1月26日”,“ 07:55”,“ 0”
"2513","01/27/2015","07:55","0" “ 2513”,“ 2015/01/27”,“ 07:55”,“ 0”
"2582","01/27/2015","07:55","0" “ 2582”,“ 2015/01/27”,“ 07:55”,“ 0”

OUTPUT: 输出:

"EmployeeCode","Date","Time","Type" “雇员代码”,“日期”,“时间”,“类型”
"3734","01/25/2015","07:54","0" “ 3734”,“ 2015年1月25日”,“ 07:54”,“ 0”
"3168","01/26/2015","07:54","0" “ 3168”,“ 2015年1月26日”,“ 07:54”,“ 0”
"4863","01/26/2015","07:55","0" “ 4863”,“ 2015年1月26日”,“ 07:55”,“ 0”
"2513","01/27/2015","07:55","0" “ 2513”,“ 2015/01/27”,“ 07:55”,“ 0”
"2582","01/27/2015","07:55","0" “ 2582”,“ 2015/01/27”,“ 07:55”,“ 0”

Suggestions, comments, sharing codes and ideas are highly appreciated. 建议,评论,共享代码和想法都受到高度赞赏。

A more typical way to do it would be to look over all the lines, extract the relevant parts, convert them to actual dates and compare them to your start and end dates with regular inequalities. 一种更典型的方法是查看所有行,提取相关部分,将它们转换为实际日期,并将它们与具有常规不等式的开始日期和结束日期进行比较。

private void button2_Click(object sender, EventArgs e)
{
    DateTime start = new DateTime(2015,01,26);
    DateTime end = new DateTime(2015,01,27);

    var oldLines = System.IO.File.ReadAllLines(txtFileName.Text);
    var newLines = oldLines
        .Select(line => new { line, date = DateTime.Parse(line.Split(',')[1]) })
        .Where(line => line.date > start and line.date < end)
        .Select(line => line.line)
        .ToArray();

    System.IO.File.WriteAllLines(txtFileName.Text, newLines);
}

This does not account for the header line, but you can handle that separately. 这不考虑标题行,但是您可以单独处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM