开发者

python convert string to datetime

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 whi开发者_如何学Ctch 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

)


Pretty weird though... In the same run locale usually doesn't change. However, if your program keeps doing this, you might want to call 'setlocale' everytime the code enters into the loop (ugly solution, I know).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜