0

I have a SQLite3 database where date and time is stored as a string like "Tue Aug 06 20:13:58 CEST 2013", and I need to extract some of the data into a CSV file.

I know how to open the database, connection, and cursor (reading), and how to open the output CSV file for writing (using the csv module).

The datetime string should be converted to the "YYYYMMDD" string -- i.e. "20130806" in the above case.

My idea was to use the datetime.datetime.strptime("Tue Aug 06 20:13:58 CEST 2013" , frm) to get the datetime object that would be later used for producing the formatted vallue. However, I get the following error:

Traceback (most recent call last):
  File "a.py", line 18, in <module>
    dt = datetime.datetime.strptime(s, frm)
  File "C:\Python33\lib\_strptime.py", line 500, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "C:\Python33\lib\_strptime.py", line 337, in _strptime
    (data_string, format))
ValueError: time data 'Tue Aug 06 20:13:51 CEST 2013' does not match format
'%a %b %m %H:%M:%S %Z %Y'

I thought it can be problem with the locale, as I am using...

>>> import locale
>>> locale.getdefaultlocale()
('cs_CZ', 'cp1250')

However, I am not able to set the locale to 'en', en_us, or the like:

>>> locale.setlocale(locale.LC_ALL, 'en')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python33\lib\locale.py", line 541, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

I need your eyes and experience.

LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114
pepr
  • 20,112
  • 15
  • 76
  • 139
  • 1
    Well, a naive approach would be to simply split the string on `" "`, extract month, date, year, map the month name to a number and concatenate the result... – l4mpi Aug 14 '13 at 11:08
  • @l4mpi: +1 I can do that, and I will probably do that. Anyway, I would like to solve the above problem anyway. – pepr Aug 14 '13 at 11:11
  • 1
    [This question](http://stackoverflow.com/questions/14547631/python-locale-error-unsupported-locale-setting) seems to answer the `locale` part - there's no locale with this name. As the locale determines the names used for days/months I think it explains the `strptime` error too. [Another answer](http://stackoverflow.com/questions/955986/what-is-the-correct-way-to-set-pythons-locale) implies Windows uses nonstandard locale names; so you'll need to find the correct name for the english locale. Oh, and you should probably not set `LC_ALL` but `LC_TIME`, which is what `strptime` uses. – l4mpi Aug 14 '13 at 11:49
  • @l4mpi: Well, the locale things seems to be a mess on Windows. Actually, it is a question whether the `datetime` approach is the correct one. I have found several other date formats used in the database that I should process. As I actually want to *fix the formated string* that were produced elsewhere, I cannot be sure I could always determine the correct locale. Please, copy your comment on the "naive approach" as the answer, so that I am able to accept it. – pepr Aug 14 '13 at 13:51
  • @l4mpi: Please, copy your comment on the "naive approach" as the answer, so that I can close the question by accepting it. ;) – pepr Aug 23 '13 at 07:13

0 Answers0