问题描述
Dateutil是以字符串格式解析日期的好工具。例如
Dateutil is a great tool for parsing dates in string format. for example
from dateutil.parser import parse
parse("Tue, 01 Oct 2013 14:26:00 -0300")
返回
datetime.datetime(2013, 10, 1, 14, 26, tzinfo=tzoffset(None, -10800))
然而,
parse("Ter, 01 Out 2013 14:26:00 -0300") # In portuguese
产生此错误:
ValueError: unknown string format
有没有人知道如何使dateutil知道该地区?
Does anybody know how to make dateutil aware of the locale?
推荐答案
您可以使用来解析:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from datetime import datetime
import icu # PyICU
df = icu.SimpleDateFormat(
'EEE, dd MMM yyyy HH:mm:ss zzz', icu.Locale('pt_BR'))
ts = df.parse(u'Ter, 01 Out 2013 14:26:00 -0300')
print(datetime.utcfromtimestamp(ts))
# -> 2013-10-01 17:26:00 (UTC)
它适用于Python 2/3。它不会修改全局状态(locale)。
It works on Python 2/3. It does not modify global state (locale).
如果您的实际输入时间字符串不包含显式的utc偏移量,则应该,否则您可能会收到错误的结果(ICU和datetime可能会使用不同的时区定义)。
If your actual input time string does not contain the explicit utc offset then you should specify a timezone to be used by ICU explicitly otherwise you can get a wrong result (ICU and datetime may use different timezone definitions).
如果您只需要支持Python 3,而您不介意设置区域设置,则可以使用 datetime.strptime()
作为:
If you only need to support Python 3 and you don't mind setting the locale then you could use datetime.strptime()
as @alexwlchan suggested:
#!/usr/bin/env python3
import locale
from datetime import datetime
locale.setlocale(locale.LC_TIME, "pt_PT.UTF-8")
print(datetime.strptime("Ter, 01 Out 2013 14:26:00 -0300",
"%a, %d %b %Y %H:%M:%S %z")) # works on Python 3.2+
# -> 2013-10-01 14:26:00-03:00
这篇关于使用dateutil.parser来解析另一种语言的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!