【问题标题】:How to specify date-format using csv2rec-method如何使用 csv2rec-method 指定日期格式
【发布时间】:2013-07-11 09:28:24
【问题描述】:

我正在使用 matplotlib.mlab.csv2rec 读取 csv 文件。第一列包含格式为

的日期
"DD.MM.YYYY  hh:mm"

只要日期大于 12,日期就会正确转换为日期时间对象。只要日期小于 12,就会被解释为月份。

例如:"13.09.2012 15:45" 被正确解释为 2012 年 9 月 13 日,而 "12.09.2012 15:45" 被解释为 2012 年 12 月 9 日,尽管它应该被解释为 2012 年 9 月 12 日。

有没有一种方法可以指定 csv2rec 方法在读取日期时应使用的日期时间格式?

【问题讨论】:

    标签: python datetime matplotlib format


    【解决方案1】:

    You can specify converting function using converterd parameter.

    以下代码假设数据时间字段是第一列。

    import datetime
    import matplotlib.mlab
    
    print matplotlib.mlab.csv2rec('data.csv', converterd={
        0: lambda x: datetime.datetime.strptime(x, '%d.%m.%Y %H:%M'),
    })
    

    【讨论】:

      猜你喜欢
      • 2012-12-09
      • 1970-01-01
      • 1970-01-01
      • 2021-07-14
      • 2018-03-17
      • 1970-01-01
      • 1970-01-01
      • 2015-07-26
      相关资源
      最近更新 更多