Python strptime芬兰语 [英] Python strptime finnish

查看:209
本文介绍了Python strptime芬兰语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个芬兰语代表,需要转换为datetime对象的日期( tiistaina,27. lokakuuta 2015 )。但是,日期和月份名称不能被Python中的datetime库识别

I have a Finnish representation of a date (tiistaina, 27. lokakuuta 2015) that I need to convert to a datetime object. However, the day and month names are not recognised by the datetime library in Python

我会期望像下面这样的工作:

I would expect something like the following to work:

import locale
from datetime import datetime

locale.setlocale(locale.LC_TIME, 'fi_FI')
the_date = datetime.strptime('tiistaina, 27. lokakuuta 2015', '%A, %d. %B %Y')

但是,这导致:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/_strptime.py", line 500, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/_strptime.py", line 337, in _strptime
(data_string, format))
ValueError: time data 'tiistaina, 27. lokakuuta 2015' does not match format '%A, %d. %B %Y'

我认为这是因为Python期待着天天而不是tiistai na ,而不是lokakuu的月份 na

I think this is because Python is expecting the day to be tiistai instead of tiistaina and the month to be lokakuu instead of lokakuuna

http://people.uta.fi/~km56049/finnish/timexp.html 似乎建议有,依赖在语境上,用芬兰语表示一个或几个月的不同方式。

http://people.uta.fi/~km56049/finnish/timexp.html seems to suggest that there are, depending on the context, different ways to represent a day or month in the Finnish language.

我如何将字符串 tiistaina,27. lokakuuta 2015 到datetime对象?

How can I the string tiistaina, 27. lokakuuta 2015 to a datetime object?

推荐答案

'%A,%d。 %B%Y'在我的系统上产生不同的时间字符串:

'%A, %d. %B %Y' produces a different time string on my system too:

#!/usr/bin/env python
import locale
from datetime import datetime

#NOTE: locale name is platform-dependent
locale.setlocale(locale.LC_TIME, 'fi_FI.UTF-8') 
print(datetime(2015, 10, 27).strftime('%A, %d. %B %Y'))
# -> tiistai, 27. lokakuu 2015






您可以使用< a href =https://pypi.python.org/pypi/PyICU =nofollow> PyICU 来解析本地化的日期/时间字符串在给定格式中:

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

tz = icu.ICUtzinfo.getDefault() # any ICU timezone will do here
df = icu.SimpleDateFormat('EEEE, dd. MMMM yyyy', icu.Locale('fi_FI'))
df.setTimeZone(tz.timezone)

ts = df.parse(u'tiistaina, 27. lokakuuta 2015')
print(datetime.fromtimestamp(ts, tz).date())
# -> 2015-10-27

相关: Python解析日期并找到正确的locale_setting

它工作,但PyICU是一个很大的依赖,你必须阅读大多数C ++文档

It works but PyICU is a big dependency and you have to read C++ docs for most things.

有一个 dateparser module ,如果您将芬兰数据添加到一个简单的yaml配置 - 类似于其他语言的完成方式。以下是荷兰语的工作示例:

There is dateparser module that should work if you add Finnish data to a simple yaml config -- similar to how it is done for other languages. Here's a working example for Dutch language:

#!/usr/bin/env python
import dateparser # $ pip install dateparser

print(dateparser.parse(u'dinsdag, 27. oktober 2015',
                       date_formats=['%A, %d. %B %Y'],
                       languages=['nl']).date())
# -> 2015-10-27

相关:在python中解析法语日期

这篇关于Python strptime芬兰语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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