【问题标题】:hot to parse csv time in this format 06:00:00;00?以这种格式 06:00:00;00 解析 csv 时间很热门吗?
【发布时间】:2013-03-30 07:18:15
【问题描述】:

我有 csv 并按行读取,因此我可以将其保存在数据库中。我正在使用 filehelper 以逗号分隔符分隔每个,但忽略“”内的那些。它正在抛出数据 06:00:00;00 的异常。我不确定那是什么。我该如何解析?那是时间跨度,但为什么它在 ; 之后有额外的 00?抱歉,数据只是给了我,没​​有解释它的用途。

这是来自文本文件的实际数据。

01/10/2013,06:00:00;00,06:09:40;08,00:09:40:09,01/10/2013,06:00:00;00,06:09:40;08,00:09:40:09,"January 9, 2013 - Dreams_01.mp4",Aired,7CFB84BD-A5B6-43E8-82EC-E78E7219B1C7

是否有用于 filhelper 的转换器?我用[FieldConverter(ConverterKind.Date, "dd/MM/yyyy")] 表示日期,但我不确定时间。

【问题讨论】:

  • 您至少需要使用您正在使用的任何编程语言进行标记
  • 我现在已经修复了标签并稍微清理了问题 - 以后提问时请多加小心 - 如果您以可读的方式提出问题,您更有可能得到好的答案带有适当标签的表单。
  • 谢谢。我只需要知道最后两个零代表什么?我知道 00:00:00 代表 hh:mm:ss ;00 是干什么用的?谢谢

标签: c# csv delimiter filehelpers


【解决方案1】:

您可以提供自己的FieldConverter,这样您就可以添加您认为必要的任何逻辑。看起来你的数据结构不是很严格,所以你可能需要玩很多,但这里有一个处理前几个字段的工作程序:

[DelimitedRecord(",")]
public partial class MyClass
{
    [FieldConverter(ConverterKind.Date, "dd/MM/yyyy")]
    public DateTime Date;
    [FieldConverter(typeof(MyTimeConverter))]
    public DateTime Time1;
    [FieldConverter(typeof(MyTimeConverter))]
    public DateTime Time2;
    [FieldConverter(typeof(MyTimeConverter))]
    public DateTime Time3;
    [FieldConverter(ConverterKind.Date, "dd/MM/yyyy")]
    public DateTime Time4;
    [FieldDelimiter("|")] // ignore the rest of the fields for this example
    public string Optional3;
}      

class Program
{
    private static void Main(string[] args)
    {
        var engine = new FileHelperEngine<MyClass>();
        var records = engine.ReadString(@"01/10/2013,06:00:00;00,06:09:40;08,00:09:40:09,01/10/2013,06:00:00;00,06:09:40;08,00:09:40:09,""January 9, 2013 - Dreams_01.mp4"",Aired,7CFB84BD-A5B6-43E8-82EC-E78E7219B1C7");
        Assert.AreEqual(records[0].Date, new DateTime(2013, 10, 1));
        Assert.AreEqual(records[0].Time1, DateTime.MinValue.Date.Add(new TimeSpan(0, 6, 0, 0)));
        Assert.AreEqual(records[0].Time2, DateTime.MinValue.Date.Add(new TimeSpan(0, 6, 9, 40, 08)));
        Assert.AreEqual(records[0].Time3, DateTime.MinValue.Date.Add(new TimeSpan(0, 0, 9, 40, 09)));
    }
}

public class MyTimeConverter : ConverterBase
{
    public override string FieldToString(object from)
    {
        return base.FieldToString(from);
    }

    public override object StringToField(string from)
    {
        /// apply any logic to clear up the input here
        /// for instance, split the string at any ';' or ':'
        var parts = from
            .Split(';', ':')
            .Select(x => Convert.ToInt32(x))
            .ToList();

        // if it has three parts assume there are no milliseconds
        if (parts.Count == 3)
            return DateTime.MinValue.Date.Add(new TimeSpan(0, parts[0], parts[1], parts[3]));
        else if (parts.Count == 4) // if it has four parts include milliseconds
            return DateTime.MinValue.Date.Add(new TimeSpan(0, parts[0], parts[1], parts[2], parts[3]));
        throw new Exception("Unexpected format");
    }
}

你可以看到我已经实现了MyTimeConverter。 (DateTime.MinValue.Date.Add(new TimeSpan()) 是我所知道的创建带有空日期部分的 DateTime 的最佳方法。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多