wong2
wong2

Reputation: 35750

Error parsing unicode date with strptime

I want to parse a date text with strptime

#-*-coding:utf-8-*-

from time import strptime

date_text = '06月13日 星期五'
print strptime(date_text, '%m月%d日 %A')

and I got this error:

ValueError: time data '06\xe6\x9c\x8813\xe6\x97\xa5 \xe6\x98\x9f\xe6\x9c\x9f\xe4\xba\x94' does not match format '%m\xe6\x9c\x88%d\xe6\x97\xa5 %A'

Upvotes: 0

Views: 161

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124100

%A only parses weekdays in the currently configured locale; by default that's always the C locale and only english weekday names are recognised.

You'd have to set the locale with the locale module, but only do that for stand-alone programs used by one user at a time.

The other option is to not have strptime parse the weekday; you could look it up in your own mapping:

daymonth, weekday = date_text.split()
date = strptime(daymonth, '%m月%d日')
weekday = weekdays_dict[weekday]

or ignore weekday altogether; the same info is already contained in the month and day.

Either way, just the month and day portion can be parsed fine:

>>> from time import strptime
>>> date_text = '06月13日 星期五'
>>> strptime(date_text.split()[0], '%m月%d日')
time.struct_time(tm_year=1900, tm_mon=6, tm_mday=13, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=164, tm_isdst=-1)

Upvotes: 1

Related Questions