【问题标题】:Get format string for date获取日期的格式字符串
【发布时间】:2017-03-30 21:47:32
【问题描述】:

我有一个日期字符串列表,可能非常大。格式未知,但在所有元素中都是相同的。假设 dateutil.parser 将成功解析它。

问题是列表非常大并且 dateutil.parser 很慢。如果我可以推断出格式字符串,我可以解析一个元素,然后使用更快的方法(也许是 strptime)。但是,据我所知,解析器没有给我它成功的格式字符串。

有没有优雅的解决方案?

【问题讨论】:

  • dateutil 目前无法实现这一点,但如果您将 infer_datetime_format 选项传递给 to_datetime()read_csv(),pandas 会执行与此非常相似的操作
  • 在 dateutil 问题跟踪器上查看问题 #125

标签: python python-dateutil


【解决方案1】:

如果您有一些繁重的计算,我认为您可以将 多线程threading 模块一起使用。

import threading

thread = threading.Thread(target=some_def, args=(), kwargs={})
thread.start() # will run some_def
thread.join() # will wait until some_def is done

或者在你的情况下:

导入线程 导入 dateutil.parser

threads = []
times = ["Sat Oct 11 17:13:46 UTC 2003", "Sat Oct 11 17:13:46 UTC 2004"]
for time in times:
    thread = threading.Thread(target=dateutil.parser.parser, args=(time))
    threads.append(thread)

for thread in threads:
    thread.start()

map(lambda thread: thread.join(), threads)

欲了解更多信息:Asynchronous method call in Python?

【讨论】:

    猜你喜欢
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    相关资源
    最近更新 更多