如何按日期删除csv文件中的多行 [英] How do I delete multiple rows in csv file by dates
问题描述
private void button2_Click(对象发件人,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()))
。选择(lineInfo = > lineInfo.Line);
}
}
从目录浏览文件后。
样本输入:
EmployeeCode,Date,Time,Type
3434,01/22/2013,07:54,0
3023,01/23/2014,07:54,0
2897,01/24/2015,07:54,0
3734,01/25/2015,07:54,0
3168,01/26/2015,07:54,0
4863,01/26/2015,07:55,0
2513,01/27/2015,07:55,0
2582,01/27/2015,07:55,0
输出:
EmployeeCode,Date,Time,Type
3734,01/25/2015,07:54 ,0
3168,01/26/2015,07:54,0
4863,01/26/2015,07:55 ,0
2513,01/27/2015,07:55,0
2582,01/27/2015,07:55 ,0
建议,评论,分享代码和想法非常感谢。
我尝试了什么:
追求我在csv文件中删除多行的目标。我的想法是获取两个日期之间的日期,这些日期不包括删除。但是我如何在两个日期之间迭代几天并获取字符串值并用于我的代码来评估包含该字段的csv文件。
我认为你的DateTime语句错误,应如下所示:
DateTime start = new DateTime( 2015 , 1 , 25 );
Console.WriteLine( Date = + start);
Console.WriteLine( Date = + start.ToShortDateString());将给予:
日期= 1/25/2015 12:00:00 AM
日期= 2015年1月25日
更新示例:
使用系统;
使用 System.Linq;
使用 System.Collections.Generic;
public class 计划
{
public static void Main()
{
DateTime start = new DateTime( 2015 , 1 , 25 );
DateTime end = new DateTime( 2015 , 1 , 27 );
var dates = new List< DateTime>();
var oldLines = new 列表< string> { 2015年1月25日, 2015年1月26日, 01 / 27/2015, 2015年1月28日};
var newLines = new List< string>();
for ( var dt = start; dt < = end; dt = dt.AddDays( 1 ))
{
dates.Add(dt );
}
foreach ( var d 日期)
{
newLines.AddRange(oldLines.Where(line = > line.Contains (d.ToShortDateString())));
}
Console.WriteLine(newLines.Count());
}
}
假设数据以空格分隔...
假设数据以双引号括起并用逗号分隔...
string [] lines = { @ EmployeeCode , 日期 , 时间 , 键入 ,
@ 3434 , 01/22 / 2013 , 07:54 , 0 ,
@ 3023 , 2014年1月23日 , 07:54 , 0 ,
@ 2897 , 2015年1月24日 , 07:54 , 0 ,
@ 3734 , 2015年1月25日 , 07:54 , 0 ,
@ 3168 < span class =code-string> , 2015年1月26日 , 07:54 , 0 ,
@ 4863 , 01/26 / 2015 , 07:55 , 0 ,
@ 2513 , 2015年1月27日 , 07:55 , 0 ,
@ 2582 , 2015年1月27日 , 07:55 < span class =code-string> , 0 };
DateTime dFm = new DateTime( 2015 , 1 , 25 );
DateTime dTo = new DateTime( 2015 , 1 , 27 );
var data = lines
.Skip( 1 )
。选择(x = > new
{
EmpCode = Int32 .Parse(x.Split( new string [] { @ , , @ },StringSplitOptions.RemoveEmptyEntries)[ 0 ]),
Date = DateTime.ParseExact(
string .Concat(x.Split( new string [] { @ , , @ },StringSplitOptions.RemoveEmptyEntries)[ 1 ], ,
x .Split( new string [] { @ , , @ },StringSplitOptions.RemoveEmptyEntries)[ 2 ]),
MM / dd / yyyy HH:mm,System.Globalization.CultureInfo.InvariantCulture),
Type = x.Split( new string [] { @ , , @ },StringSplitOptions.RemoveEmptyEntries)[ 3 ]
})
.Where(x => x.Date> = dFm&& x.Date< = dTo)
.ToList();
结果:
< pre lang =text> 3734 2015-01-25 07:54:00 0
3168 2015-01-26 07:54:00 0
4863 2015-01-26 07:55:00 0
祝你好运!
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);
}
}
After browsing the file from directory.
SAMPLE INPUT:
"EmployeeCode","Date","Time","Type"
"3434","01/22/2013","07:54","0"
"3023","01/23/2014","07:54","0"
"2897","01/24/2015","07:54","0"
"3734","01/25/2015","07:54","0"
"3168","01/26/2015","07:54","0"
"4863","01/26/2015","07:55","0"
"2513","01/27/2015","07:55","0"
"2582","01/27/2015","07:55","0"
OUTPUT:
"EmployeeCode","Date","Time","Type"
"3734","01/25/2015","07:54","0"
"3168","01/26/2015","07:54","0"
"4863","01/26/2015","07:55","0"
"2513","01/27/2015","07:55","0"
"2582","01/27/2015","07:55","0"
Suggestions, comments, sharing codes and ideas are highly appreciated.
What I have tried:
To pursue my goal of deleting multiple lines in csv file. 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.
I think your DateTime statement is wrong and should look like this:
DateTime start = new DateTime(2015, 1, 25); Console.WriteLine("Date = " + start); Console.WriteLine("Date = " + start.ToShortDateString());Will give:
Date = 1/25/2015 12:00:00 AM
Date = 1/25/2015
Updated Example:
using System; using System.Linq; using System.Collections.Generic; public class Program { public static void Main() { DateTime start = new DateTime(2015, 1, 25); DateTime end = new DateTime(2015, 1, 27); var dates = new List<DateTime>(); var oldLines = new List<string> { "01/25/2015", "01/26/2015", "01/27/2015", "01/28/2015" }; var newLines = new List<string>(); for (var dt = start; dt <= end; dt = dt.AddDays(1)) { dates.Add(dt); } foreach(var d in dates) { newLines.AddRange(oldLines.Where(line => line.Contains(d.ToShortDateString()))); } Console.WriteLine(newLines.Count()); } }
Assuming that data are delimited by space...
Assuming that data are arounded by double quote and delimited by comma...
string[] lines = {@"""EmployeeCode"",""Date"",""Time"",""Type""", @"""3434"",""01/22/2013"",""07:54"",""0""", @"""3023"",""01/23/2014"",""07:54"",""0""", @"""2897"",""01/24/2015"",""07:54"",""0""", @"""3734"",""01/25/2015"",""07:54"",""0""", @"""3168"",""01/26/2015"",""07:54"",""0""", @"""4863"",""01/26/2015"",""07:55"",""0""", @"""2513"",""01/27/2015"",""07:55"",""0""", @"""2582"",""01/27/2015"",""07:55"",""0"""}; DateTime dFm = new DateTime(2015, 1, 25); DateTime dTo = new DateTime(2015, 1, 27); var data = lines .Skip(1) .Select(x=> new { EmpCode = Int32.Parse(x.Split(new string[]{@""",""", @""""}, StringSplitOptions.RemoveEmptyEntries)[0]), Date = DateTime.ParseExact( string.Concat(x.Split(new string[]{@""",""", @""""}, StringSplitOptions.RemoveEmptyEntries)[1], " ", x.Split(new string[]{@""",""", @""""}, StringSplitOptions.RemoveEmptyEntries)[2]), "MM/dd/yyyy HH:mm", System.Globalization.CultureInfo.InvariantCulture), Type = x.Split(new string[]{@""",""", @""""}, StringSplitOptions.RemoveEmptyEntries)[3] }) .Where(x=>x.Date>=dFm && x.Date<=dTo) .ToList();
Result:
3734 2015-01-25 07:54:00 0 3168 2015-01-26 07:54:00 0 4863 2015-01-26 07:55:00 0
Good luck!
这篇关于如何按日期删除csv文件中的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!