我有一个 CSV 文件,其中日期和时间分为 2 个字段。如何使用 FileHelpers 将 2 个字段聚合到相同的 DateTime 数据中?

发布于 2024-10-22 02:35:43 字数 792 浏览 6 评论 0原文

我有一个 CSV 文件,其中日期和时间分为 2 个字段。 如何使用 FileHelpers 将 2 个字段聚合到相同的 DateTime 数据中?

谢谢,

2011.01.07,09:56,1.2985,1.2986,1.2979,1.2981,103
2011.01.07,09:57,1.2981,1.2982,1.2979,1.2982,75
2011.01.07,09:58,1.2982,1.2982,1.2976,1.2977,83
2011.01.07,09:59,1.2977,1.2981,1.2977,1.2980,97
2011.01.07,10:00,1.2980,1.2980,1.2978,1.2979,101
2011.01.07,10:01,1.2980,1.2981,1.2978,1.2978,57
2011.01.07,10:02,1.2978,1.2979,1.2977,1.2978,86
2011.01.07,10:03,1.2978,1.2978,1.2973,1.2973,84
2011.01.07,10:04,1.2973,1.2976,1.2973,1.2975,71
2011.01.07,10:05,1.2974,1.2977,1.2974,1.2977,53
2011.01.07,10:06,1.2977,1.2979,1.2976,1.2978,57
2011.01.07,10:07,1.2978,1.2978,1.2976,1.2976,53
2011.01.07,10:08,1.2976,1.2980,1.2976,1.2980,58
2011.01.07,10:09,1.2979,1.2985,1.2979,1.2980,63

I have a CSV file where the Date and the time are into 2 fields.
How can I use FileHelpers to aggregate the 2 fields into the same DateTime data ?

Thanks,

2011.01.07,09:56,1.2985,1.2986,1.2979,1.2981,103
2011.01.07,09:57,1.2981,1.2982,1.2979,1.2982,75
2011.01.07,09:58,1.2982,1.2982,1.2976,1.2977,83
2011.01.07,09:59,1.2977,1.2981,1.2977,1.2980,97
2011.01.07,10:00,1.2980,1.2980,1.2978,1.2979,101
2011.01.07,10:01,1.2980,1.2981,1.2978,1.2978,57
2011.01.07,10:02,1.2978,1.2979,1.2977,1.2978,86
2011.01.07,10:03,1.2978,1.2978,1.2973,1.2973,84
2011.01.07,10:04,1.2973,1.2976,1.2973,1.2975,71
2011.01.07,10:05,1.2974,1.2977,1.2974,1.2977,53
2011.01.07,10:06,1.2977,1.2979,1.2976,1.2978,57
2011.01.07,10:07,1.2978,1.2978,1.2976,1.2976,53
2011.01.07,10:08,1.2976,1.2980,1.2976,1.2980,58
2011.01.07,10:09,1.2979,1.2985,1.2979,1.2980,63

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

守望孤独 2024-10-29 02:35:43
var file = @"2011.01.07,09:56,1.2985,1.2986,1.2979,1.2981,103
2011.01.08,09:57,1.2981,1.2982,1.2979,1.2982,75
2011.01.09,09:58,1.2982,1.2982,1.2976,1.2977,83
2011.01.07,09:59,1.2977,1.2981,1.2977,1.2980,97
2011.01.07,10:00,1.2980,1.2980,1.2978,1.2979,101
2011.01.07,10:01,1.2980,1.2981,1.2978,1.2978,57
2011.01.07,10:02,1.2978,1.2979,1.2977,1.2978,86
2011.01.07,10:03,1.2978,1.2978,1.2973,1.2973,84
2011.01.07,10:04,1.2973,1.2976,1.2973,1.2975,71
2011.01.07,10:05,1.2974,1.2977,1.2974,1.2977,53
2011.01.07,10:06,1.2977,1.2979,1.2976,1.2978,57
2011.01.07,10:07,1.2978,1.2978,1.2976,1.2976,53
2011.01.07,10:08,1.2976,1.2980,1.2976,1.2980,58
2011.01.07,10:09,1.2979,1.2985,1.2979,1.2980,63";

var rows = file.Split('\n');
foreach(var row in rows){
    var cols = row.Split(',');
    var col1 = new DateTime();
    foreach(var col in cols){
        if (col == cols[0]) // first col
        {
            var dateParts = col.Split('.');
            col1 = new DateTime(int.Parse(dateParts[0]), int.Parse(dateParts[1]), int.Parse(dateParts[2]));
        }
        else if (col == cols[1]) // second col
        {
            var timeParts = col.Split(':');
            col1 = col1.AddHours(int.Parse(timeParts[0]));
            col1 = col1.AddMinutes(int.Parse(timeParts[1]));
            //col1.Dump();
        }
        else {
                // all other columns here
            }
    }
}
var file = @"2011.01.07,09:56,1.2985,1.2986,1.2979,1.2981,103
2011.01.08,09:57,1.2981,1.2982,1.2979,1.2982,75
2011.01.09,09:58,1.2982,1.2982,1.2976,1.2977,83
2011.01.07,09:59,1.2977,1.2981,1.2977,1.2980,97
2011.01.07,10:00,1.2980,1.2980,1.2978,1.2979,101
2011.01.07,10:01,1.2980,1.2981,1.2978,1.2978,57
2011.01.07,10:02,1.2978,1.2979,1.2977,1.2978,86
2011.01.07,10:03,1.2978,1.2978,1.2973,1.2973,84
2011.01.07,10:04,1.2973,1.2976,1.2973,1.2975,71
2011.01.07,10:05,1.2974,1.2977,1.2974,1.2977,53
2011.01.07,10:06,1.2977,1.2979,1.2976,1.2978,57
2011.01.07,10:07,1.2978,1.2978,1.2976,1.2976,53
2011.01.07,10:08,1.2976,1.2980,1.2976,1.2980,58
2011.01.07,10:09,1.2979,1.2985,1.2979,1.2980,63";

var rows = file.Split('\n');
foreach(var row in rows){
    var cols = row.Split(',');
    var col1 = new DateTime();
    foreach(var col in cols){
        if (col == cols[0]) // first col
        {
            var dateParts = col.Split('.');
            col1 = new DateTime(int.Parse(dateParts[0]), int.Parse(dateParts[1]), int.Parse(dateParts[2]));
        }
        else if (col == cols[1]) // second col
        {
            var timeParts = col.Split(':');
            col1 = col1.AddHours(int.Parse(timeParts[0]));
            col1 = col1.AddMinutes(int.Parse(timeParts[1]));
            //col1.Dump();
        }
        else {
                // all other columns here
            }
    }
}
空‖城人不在 2024-10-29 02:35:43

我回答我自己的问题。
因为我的需求发生了变化,所以我并没有真正回答我自己的问题。
我的问题已经演变为:
我有一个 CSV 文件,其中日期和时间分为 2 个字段。如何使用 FileHelpers 将 2 个字段聚合到相同的 XLDate 数据中?

这是我使用此代码的完整项目:
在此处输入链接说明

        private void CreateGraphic(ZedGraphControl zgc)
    {
        // référence vers le "canevas"
        GraphPane pane = zgc.GraphPane;

        pane.Title.Text = "Japanese Candlestick Chart Demo";
        pane.XAxis.Title.Text = "Trading Date";
        pane.YAxis.Title.Text = "Share Price, $US";

        FileHelperEngine<MetaTrader4> engine = new FileHelperEngine<MetaTrader4>();

        engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue;

        MetaTrader4[] res = engine.ReadFile(@"..\..\Data\EURUSD240.csv");

        if (engine.ErrorManager.ErrorCount > 0)
            engine.ErrorManager.SaveErrors("Errors.txt");

        StockPointList spl = new StockPointList();

        foreach (MetaTrader4 quotes in res)
        {
            DateTime dateTime = new DateTime();
            dateTime = DateTime.ParseExact(quotes.Date + "-" + quotes.Time, "yyyy.MM.dd-HH:mm",
                                             null);

            XDate xDate = new XDate(dateTime);
            StockPt pt = new StockPt(xDate, quotes.Hight, quotes.Low, quotes.Open, quotes.Close, quotes.Volume);
            spl.Add(pt);
        }

        JapaneseCandleStickItem myCurve = pane.AddJapaneseCandleStick("trades", spl);
        myCurve.Stick.IsAutoSize = true;
        myCurve.Stick.Color = Color.Blue;

        // Use DateAsOrdinal to skip weekend gaps
        pane.XAxis.Type = AxisType.DateAsOrdinal;

        // pretty it up a little
        pane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f);
        pane.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), 45.0f);

        zgc.AxisChange();

    }

I answer my own question.
Because my need have change, I dont really answer my own question.
My question have evolve to :
I have a CSV file where the Date and the time are into 2 fields. How can I use FileHelpers to aggregate the 2 fields into the same XLDate data ?

Here the full projet where I use this code :
enter link description here

        private void CreateGraphic(ZedGraphControl zgc)
    {
        // référence vers le "canevas"
        GraphPane pane = zgc.GraphPane;

        pane.Title.Text = "Japanese Candlestick Chart Demo";
        pane.XAxis.Title.Text = "Trading Date";
        pane.YAxis.Title.Text = "Share Price, $US";

        FileHelperEngine<MetaTrader4> engine = new FileHelperEngine<MetaTrader4>();

        engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue;

        MetaTrader4[] res = engine.ReadFile(@"..\..\Data\EURUSD240.csv");

        if (engine.ErrorManager.ErrorCount > 0)
            engine.ErrorManager.SaveErrors("Errors.txt");

        StockPointList spl = new StockPointList();

        foreach (MetaTrader4 quotes in res)
        {
            DateTime dateTime = new DateTime();
            dateTime = DateTime.ParseExact(quotes.Date + "-" + quotes.Time, "yyyy.MM.dd-HH:mm",
                                             null);

            XDate xDate = new XDate(dateTime);
            StockPt pt = new StockPt(xDate, quotes.Hight, quotes.Low, quotes.Open, quotes.Close, quotes.Volume);
            spl.Add(pt);
        }

        JapaneseCandleStickItem myCurve = pane.AddJapaneseCandleStick("trades", spl);
        myCurve.Stick.IsAutoSize = true;
        myCurve.Stick.Color = Color.Blue;

        // Use DateAsOrdinal to skip weekend gaps
        pane.XAxis.Type = AxisType.DateAsOrdinal;

        // pretty it up a little
        pane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f);
        pane.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), 45.0f);

        zgc.AxisChange();

    }
樱&纷飞 2024-10-29 02:35:43

您可以通过侦听 engine.BeforeReadRecord 事件来完成此操作。

当您(引擎)读取每一行时,您需要删除日期和时间之间的分隔符,以便它符合您在 FieldConverter 属性中指定的预期日期时间格式。

这是一个用于演示的 linqpad 片段。

void Main()
{
    string reading = "2011.01.07,09:56,1.2985,1.2986,1.2979,1.2981,103";

    var engine = new FileHelperEngine<Reading>();

//engine.Options.Fields.Dump();

    char delimiter = ((DelimitedRecordOptions)engine.Options).Delimiter.ToCharArray().First();

    int expectedDelimiterCount = engine.Options.Fields.Sum(field => field.FieldType == typeof(DateTime) ? 2 : field.ArrayMinLength == 0 ? 1 : field.ArrayMinLength);

    expectedDelimiterCount--; // no ending delimiter

    engine.BeforeReadRecord += (ngin, e) => {

        int fieldCount = e.RecordLine.Count(c => c == delimiter);

        if (fieldCount == expectedDelimiterCount)
        {
            int delimiterIndex = e.RecordLine.IndexOf(delimiter);

            if (delimiterIndex > NOT_FOUND)
            {
                e.RecordLine = e.RecordLine.Remove(delimiterIndex, 1);          
            }
        }

    };


    var readings = engine.ReadString(reading);

    readings.Dump();

}

const int NOT_FOUND = -1;

// Define other methods and classes here
[DelimitedRecord(",")]
class Reading
{
    [FieldOrder(1)] 
    [FieldConverter(ConverterKind.Date, "yyyy.MM.ddHH:mm")]
    public DateTime CollectionDate { get; set; }

    [FieldOrder(2)] 
    [FieldArrayLength(4)]
    public decimal[] Data;

    [FieldOrder(3)] 
    public int CollectorID { get; set; }
}

将 DateTime 属性写到两个字段中作为读者的练习。

You do this by listening to the engine.BeforeReadRecord event.

As you (engine) read each line, you'll need to remove the delimiter between the date and time so that it fits the expected date time format you specified in the FieldConverter attribute.

Here's is a linqpad snippet to demonstrate.

void Main()
{
    string reading = "2011.01.07,09:56,1.2985,1.2986,1.2979,1.2981,103";

    var engine = new FileHelperEngine<Reading>();

//engine.Options.Fields.Dump();

    char delimiter = ((DelimitedRecordOptions)engine.Options).Delimiter.ToCharArray().First();

    int expectedDelimiterCount = engine.Options.Fields.Sum(field => field.FieldType == typeof(DateTime) ? 2 : field.ArrayMinLength == 0 ? 1 : field.ArrayMinLength);

    expectedDelimiterCount--; // no ending delimiter

    engine.BeforeReadRecord += (ngin, e) => {

        int fieldCount = e.RecordLine.Count(c => c == delimiter);

        if (fieldCount == expectedDelimiterCount)
        {
            int delimiterIndex = e.RecordLine.IndexOf(delimiter);

            if (delimiterIndex > NOT_FOUND)
            {
                e.RecordLine = e.RecordLine.Remove(delimiterIndex, 1);          
            }
        }

    };


    var readings = engine.ReadString(reading);

    readings.Dump();

}

const int NOT_FOUND = -1;

// Define other methods and classes here
[DelimitedRecord(",")]
class Reading
{
    [FieldOrder(1)] 
    [FieldConverter(ConverterKind.Date, "yyyy.MM.ddHH:mm")]
    public DateTime CollectionDate { get; set; }

    [FieldOrder(2)] 
    [FieldArrayLength(4)]
    public decimal[] Data;

    [FieldOrder(3)] 
    public int CollectorID { get; set; }
}

Writing out the DateTime property into two fields is left as an exercise for the reader.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文