0

can't understand why strptime() isn't parsing the month name string, I am sure I am comfused with something but i stared at it for long time and couldn't figure it out... My code is like this:

    monthDay = todayDate[0].split(' ')
    monthDay
Output: ['28', 'januari', '2022', '08:56', 'GMT−5']

# Handling of US/EU date formats
if len(monthDay) == 2:
    year = int(todayDate[1])
    month = monthDay[0]
    day = int(monthDay[1])
else:
    year = int(monthDay[2])
    month = monthDay[1]
    day = int(monthDay[0])

    month
    Output: 'januari'

todayDate = datetime.strptime(month,'%B')

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-52-e8fbf5b845ac> in <module>
----> 1 todayDate = datetime.strptime(month,'%B')

~\Anaconda3\envs\general\lib\_strptime.py in _strptime_datetime(cls, data_string, format)
    566     """Return a class cls instance based on the input string and the
    567     format string."""
--> 568     tt, fraction, gmtoff_fraction = _strptime(data_string, format)
    569     tzname, gmtoff = tt[-2:]
    570     args = tt[:6] + (fraction,)

~\Anaconda3\envs\general\lib\_strptime.py in _strptime(data_string, format)
    347     found = format_regex.match(data_string)
    348     if not found:
--> 349         raise ValueError("time data %r does not match format %r" %
    350                          (data_string, format))
    351     if len(data_string) != found.end():

ValueError: time data 'januari' does not match format '%B'
Sam.H
  • 193
  • 2
  • 14
  • 1
    I'm not sure that the `datetime` module will handle non-English spellings of dates. The English month is "January" – Adam Smith Feb 23 '22 at 19:33
  • 2
    Did you [set the locale](https://stackoverflow.com/questions/955986/what-is-the-correct-way-to-set-pythons-locale-on-windows/956084#956084) correctly? Or at all? – Random Davis Feb 23 '22 at 19:38
  • 1
    @AdamSmith Thanks, I didn't notice the month name is converted to Swedish, I am using CBOE data, so I assumed its by default English...thanks for pointing it out, it solved my problem – Sam.H Feb 23 '22 at 19:44
  • @RandomDavis No I didn't set it at all, i thought I should let it automatically figure that out by itself, maybe I am wrong then? – Sam.H Feb 23 '22 at 19:45

1 Answers1

0

It was the string name 'januari' not January as pointed by Adam

Sam.H
  • 193
  • 2
  • 14