python将字符串转换为datetime [英] python convert string to datetime

查看:478
本文介绍了python将字符串转换为datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环,我尝试处理一组数据,其中一个操作是将普通字符串转换为datetime。一切工作正常,除非有时发生一个奇怪的事情...这里是我所知道的




  • 有完全相同的参数输入功能总是

  • 这些参数始终是相同的类型

  • 第一次运行它,它总是得到

  • 它在appx中的第二个元素80%的抛出和值错误(时间数据不匹配格式)

  • ,但是在我再次运行之后,一切都可以,它被卡住在下一个发票...



因为我的功能很大,有很多事情,我决定为你提供一些树苗代码我在这里写的只是为了澄清:

  data = ['1999年1月20日','2010年3月4日' 6月11日1819'] 
dformat ='%B%d%Y'

数据中的项目:
out = datetime.datetime.strptime(item,dformat)
打印出来

虽然这很清楚我的程序中的orks没有...我已经尝试了所有我想出来但没有成功,所以我会很高兴与任何想法你提供谢谢



btw:错误我总是看起来像这样

  ValueError:时间数据不匹配格式:data = 2010年3月4日fmt =%B %d%Y 


解决方案

您可能有不同的区域设置向上。 %B是3月份使用英语的地区,但在其他地区则会失败。



例如:

 >>>导入区域设置
>>> locale.setlocale(locale.LC_ALL,'sv_SE.utf8')
'sv_SE.utf8'
>>> import datetime
>>>>
>>> data = ['1999年1月20日','2010年3月4日','1819年6月11日']
>>>对于数据中的项目:
... print datetime.datetime.strptime(item,'%B%d%Y')
...
追溯(最近的最后一次调用):
文件< stdin>,第2行,在< module>
文件/usr/lib/python2.6/_strptime.py,第325行,_strptime
(data_string,format))
ValueError:时间数据'1999年1月20日'匹配格式'%B%d%Y'

您可以看到,即使格式是/否匹配,它声称它不。这是因为月份名称不匹配。将其更改为瑞典语区域名称,并重新起作用:

 >>>导入区域设置
>>> locale.setlocale(locale.LC_ALL,'sv_SE.utf8')
'sv_SE.utf8'
>>> import datetime
>>>>
>>> data = ['Januari 20 1999','Mars 4 2010','Juni 11 1819']
>>>对于数据中的项目:
... print datetime.datetime.strptime(item,'%B%d%Y')
...
1999-01-20 00:00: 00
2010-03-04 00:00:00
1819-06-11 00:00:00

(请注意,上述区域设置sv_SE.utf8可能不适用于您,因为您必须安装特定的区域设置。要查看在Unix机器上安装的区域设置,请运行此命令从命令行:

  $ locale -a 
C
en_AG
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IN
en_NG
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZW.utf8
POSIX
sv_FI.utf8
sv_SE.utf8


i have a loop where i try to process set of data where one action is to convert ordinary string to datetime. everything works fine except sometimes happend a weird thing ... here is what i know

  • there are exactly the same parameters entering the function always
  • those parameters are the same type always
  • first time i run it, it always get trought
  • when it gets to second element in the loop in appx 80% throw and value error (time data did not match format)
  • but after i run it again, everything is ok, and it gets stuck on next emelement ...

because my function is pretty big and there are many things happing i decide to provide you with some saple code whitch i wrote right here, just for clarification:

data = ['January 20 1999', 'March 4 2010', 'June 11 1819']
dformat = '%B %d %Y'

for item in data:
    out = datetime.datetime.strptime(item, dformat)
    print out

although this clearly works in my program it doesnt ... i have try everything i have came up with but havent succeeded yet therefore i would be glad with any idea you provide thanks

btw: the error i always get looks like this

ValueError: time data did not match format:  data=March 4 2010  fmt=%B %d %Y

解决方案

You probably have a different locale set up. %B is March in locales that use English, but in other locales it will fail.

For example:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'sv_SE.utf8')
'sv_SE.utf8'
>>> import datetime
>>> 
>>> data = ['January 20 1999', 'March 4 2010', 'June 11 1819']
>>> for item in data:
...     print datetime.datetime.strptime(item, '%B %d %Y')
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/usr/lib/python2.6/_strptime.py", line 325, in _strptime
    (data_string, format))
ValueError: time data 'January 20 1999' does not match format '%B %d %Y'

Here you see that even though the format does match, it claims it doesn't. And that's because the month names doesn't match. Change it to Swedish locale names, and it works again:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'sv_SE.utf8')
'sv_SE.utf8'
>>> import datetime
>>> 
>>> data = ['Januari 20 1999', 'Mars 4 2010', 'Juni 11 1819']
>>> for item in data:
...     print datetime.datetime.strptime(item, '%B %d %Y')
... 
1999-01-20 00:00:00
2010-03-04 00:00:00
1819-06-11 00:00:00

(Note that the above locale 'sv_SE.utf8' might not work for you, because you have to have that specific locale installed. To see which ones that are installed on a Unix machine, run this command from the command line:

$ locale -a
C
en_AG
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IN
en_NG
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZW.utf8
POSIX
sv_FI.utf8
sv_SE.utf8

)

这篇关于python将字符串转换为datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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