如何在C#中按日期范围过滤csv行 [英] How do I filter csv lines by date range in C#

查看:147
本文介绍了如何在C#中按日期范围过滤csv行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用csvHelper过滤csv文件。我有一个代码,但我不知道如何在if语句中处理过滤掉的记录。我想再次只写日期范围内的行。



我尝试过:



I'm using csvHelper in filtering csv files. I had a code below but I don't know how to process the filtered out records in if statements. I want to write it again only the lines within the date range.

What I have tried:

var dFm = new DateTime(2015, 01, 25);
var dTo = new DateTime(2015, 01, 27);
using (var reader = File.OpenText(@"sample.csv"))
{
    var csv = new CsvReader(reader);
    while (csv.Read())
    {
        var date = csv.GetField<DateTime>("Date");
        if (date >= dFm &&
            date <= dTo)
        {
            // process the filtered out records
        }
    }
}

推荐答案

我从未使用过CsvHelper,但在访问GitHub页面上的问题时,我发现了一个问题:

多种日期格式支持·问题#603·JoshClose / CsvHelper·GitHub [ ^ ]



阅读之后,我认为你可能需要使用以下内容:

I never used CsvHelper, but on visiting the Issues on the GitHub page I noticed an issue:
Multiple date format support · Issue #603 · JoshClose/CsvHelper · GitHub[^]

After reading that I think for dates might you need to use something like:
TypeConverterOption("yyyyMMdd")



更新的答案:


Updated answer:

var dFm = new DateTime(2015, 01, 25);
            var dTo = new DateTime(2015, 01, 27);

            using (var reader = File.OpenText(@"sample.csv"))
            {
                var csv = new CsvReader(reader);

                while (csv.Read())
                {
                    var date = csv.GetField<string>("Date");
                    // See: https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings
                    var date2 = DateTime.ParseExact(date, "MM/dd/yyyy", null);

                    if (date2 >= dFm && date2 <= dTo)
                    {
                        // process the filtered out records
                        Debug.Print(date2.ToLongDateString());
                    }
                }
            }


我认为您需要的是要了解csv文件最初只是'a Comma Seperated Verdices的文件。实际上它实际上并不一定是由逗号分隔的,它通过System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator中设置的值分隔在线程上



一旦你想一想,从任何读者阅读和处理的内容中获取价值应该是直截了当的。如果你正在使用Github的开源CSV阅读器并且它不支持你想要的东西,只需将它扩展并扩展即可。



本质上你'将要写入一个绑定到您正在阅读的同一文件的流,只记得在写之前关闭它并且几乎不会出错:)
I think what you need is to appreciate that a csv file initially is simply 'a file of Comma Seperated Verdices'. In detail actually it's not necessarily seperated by Comma, it is seperated on the thread by the value set in System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator

Once you think of that, getting the value out of what you have read and treated with any sort of reader should be straight forward. If you're using the open source CSV reader from Github and it doesn't support what you want, simply fork and extend it to be able.

In essence you're going to be writing to a stream that binds to the same file you've been reading, just remember to close it before writing and it can hardly go wrong :)


这篇关于如何在C#中按日期范围过滤csv行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆