Python解析日期并找到正确的locale_setting [英] Python parsing date and find the correct locale_setting

查看:201
本文介绍了Python解析日期并找到正确的locale_setting的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下日期字符串:'3févr。 2015 14:26:00 CET'

  datetime.datetime.strptime('03févr。2015 14:26:00' '%d%b%Y%H:%M:%S')

错误:

  ValueError:时间数据'03 f\xc3\xa9vr。 2015 14:26:00'不符合格式'%d%b%Y%H:%M:%S'

我试图循环使用 locale.locale_alias 的所有地区:

  for l in locale.locale_alias:
try:
locale.setlocale(locale.LC_TIME,l)
print l,datetime.datetime.strptime('03févr。2015 14:26:00','%d%b%Y%H:%M:%S')
break
除了例外为e:
print e

但我找不到正确的。

解决方案

要解析本地化的日期/时间字符串,请使用 ICU日期/时间格式

 #!/ usr / bin / env python 
# - * - 编码:utf-8 - * -
from datetime import datetime
import icu#PyICU
import pytz#$ pip install pytz

tz = icu.ICUtzinfo.getDefault()#any ICU时区将在这里
df = icu.DateFormat.createDateTimeInstance(icu.DateFormat.MEDIUM,
icu.DateFormat.MEDIUM,
icu.Locale.getFrench())
df.setTimeZone(tz.timezone)

ts = df.parse(u'3févr。 2015 14:26:00 CET')#注意:CET被忽略
naive_dt = datetime.fromtimestamp(ts,tz).replace(tzinfo = None)
dt = pytz.timezone('Europe / Paris ').localize(naive_dt,is_dst = None)
print(dt)# - > 2015-02-03 14:26:00 + 01:00

df .applyPattern()可用于设置不同的日期/时间模式( df.toPattern()),或者您可以使用 icu.SimpleDateFormat df 格式和区域设置直接



有必要使用显式的ICU时区(以便 df.parse() .fromtimestamp()可以使用相同的utc偏移量),因为 icu datetime 可能会使用不同的时区定义。



pytz 为过去/未来的日期获得适当的UTC偏移量(某些时区可能在过去/将来具有不同的utc偏移量,包括与DST转换无关的原因)。


I have the following date string: '3 févr. 2015 14:26:00 CET'

datetime.datetime.strptime('03 févr. 2015 14:26:00', '%d %b %Y %H:%M:%S')

Parsing this failed with the error:

ValueError: time data '03 f\xc3\xa9vr. 2015 14:26:00' does not match format '%d %b %Y %H:%M:%S'

I tried to loop over all locales with locale.locale_alias:

for l in locale.locale_alias:
    try:
        locale.setlocale(locale.LC_TIME, l)
        print l,datetime.datetime.strptime('03 févr. 2015 14:26:00', '%d %b %Y %H:%M:%S')
        break
    except Exception as e:
        print e

but I was not able to find the correct one.

解决方案

To parse localized date/time string using ICU date/time format:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from datetime import datetime
import icu  # PyICU
import pytz # $ pip install pytz

tz = icu.ICUtzinfo.getDefault() # any ICU timezone will do here
df = icu.DateFormat.createDateTimeInstance(icu.DateFormat.MEDIUM,
                                           icu.DateFormat.MEDIUM,
                                           icu.Locale.getFrench())
df.setTimeZone(tz.timezone)

ts = df.parse(u'3 févr. 2015 14:26:00 CET') #NOTE: CET is ignored
naive_dt = datetime.fromtimestamp(ts, tz).replace(tzinfo=None)
dt = pytz.timezone('Europe/Paris').localize(naive_dt, is_dst=None)
print(dt) # -> 2015-02-03 14:26:00+01:00

df.applyPattern() could be used to set a different date/time pattern (df.toPattern()) or you could use icu.SimpleDateFormat to get df from the format and the locale directly.

It is necessary to use an explicit ICU timezone (so that df.parse() and .fromtimestamp() could use the same utc offset) because icu and datetime may use different timezone definitions.

pytz is used here, to get a proper UTC offset for past/future dates (some timezones may have different utc offsets in the past/future including reasons unrelated to DST transitions).

这篇关于Python解析日期并找到正确的locale_setting的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆