What to do if you get `locale.Error: unsupported locale setting` when getting the day of the week from a date in Python

I want to get the day of the week from the date in Python

Image like below 2019-01-07 00:00:00⇒Tuesday

Execution environment

What i did

This article seemed to be helpful, so I almost copied and executed it. Get the day and month from the date in Python as a string (Japanese, English, etc.)

It seems that you can get it by changing the locale with the locale module.

import datetime
import locale

dt = datetime.datetime(2018, 1, 1)
print(dt)
# 2018-01-01 00:00:00

print(dt.strftime('%A, %a, %B, %b'))
# Monday, Mon, January, Jan
locale.setlocale(locale.LC_TIME, 'ja_JP.UTF-8')
print(locale.getlocale(locale.LC_TIME))
# ('ja_JP', 'UTF-8')
print(dt.strftime('%A, %a, %B, %b'))
#Monday,Month,January,  1

There was a reference code like the one above, so when I ran it, I got an error ...

2018-01-01 00:00:00
Monday, Mon, January, Jan
Traceback (most recent call last):
  File ".\time_test.py", line 11, in <module>
    locale.setlocale(locale.LC_TIME, 'ja_JP.UTF-8')
  File "C:\Users\XXXXX\AppData\Local\Programs\Python\Python37\lib\locale.py", line 604, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

Since it says locale.Error: unsupported locale setting, I feel that the language and region specified by setlocale are incorrect.

Error handling

Check the arguments passed by setlocale properly. locale --- Python documentation First of all, it says to write the following code.

import locale
locale.setlocale(locale.LC_ALL, '')

If you change the part of locale.setlocale (locale.LC_TIME,'ja_JP.UTF-8') to the above writing style ...

time_test.py


import datetime
import locale

dt = datetime.datetime(2018, 1, 1)
print(dt)
# 2018-01-01 00:00:00

print(dt.strftime('%A, %a, %B, %b'))
# Monday, Mon, January, Jan

#locale.setlocale(locale.LC_TIME, 'ja-JP')
locale.setlocale(locale.LC_ALL, '')
print(locale.getlocale(locale.LC_TIME))


print(dt.strftime('%A, %a, %B, %b'))
#Monday,Month,January,  1

Output result

2018-01-01 00:00:00
Monday, Mon, January, Jan
('Japanese_Japan', '932')
Monday,Month,January, 1

I was able to display the day of the week I wanted to get.

Since the output result is ('Japanese_Japan', '932') in print (locale.getlocale (locale.LC_TIME)), it is presumed that the cause was specified by ja_JP.

locale.setlocale (locale.LC_ALL,'') Even if I changed it to locale.setlocale (locale.LC_TIME,'Japanese_Japan.UTF-8'), it seemed that I could get Japanese results, so I executed it.

('Japanese_Japan', 'utf8')
Monday,Month,January, 1

I was able to get the expected results.

Recommended Posts

What to do if you get `locale.Error: unsupported locale setting` when getting the day of the week from a date in Python
What to do if you get a minus zero in Python
What to do if you get a "Wrong Python Platform" warning when using Python with the NetBeans IDE
[Python] What to do if you get a ModuleNotFoundError when importing pandas using Jupyter Notebook in Anaconda
What to do if you get a memory error when converting from PySparkDataFrame to PandasDataFrame
What to do if you get an error when importing matplotlib in Python (Mac)
What to do if you get the error RuntimeError: Python is not installed as a framework when trying to use matplitlib and pylab in Python 3.3
What to do if you get a must override `get_config` error when trying to model.save in Keras
[Python] What to do when PEP8 is violated in the process of importing from the directory added to sys.path
What to do if you get an error when installing python with pyenv
What to do if you get "Python not configured." Using PyDev in Eclipse
[Python] What to check when you get a Unicode Decode Error in Django
What to do if you get an error when trying to send a message in tasks.loop () immediately after startup
What to do if you get an OpenSSL error when installing Python 2 with pyenv
What to do if you get `No kernel for language python found` in Hydrogen
What to do if you get "(35,'SSL connect error')" in pycurl (one of them)
What to do if the progress bar is not displayed in tqdm of python
What to do if Python does not switch from the System version in pyenv
What to do if you get the error ʻERR_FEATURE_UNAVAILABLE_ON_PLATFORM` when using ts-node-dev on Linux
What to do if you can't hit the arrow keys in the Python interactive console
What to do if you get "coverage unknown" in Coveralls
What to do if you get the error "Error: opencv3: Does not support building both Python 2 and 3 wrappers" when installing openCV 3
Make a note of what you want to do in the future with Raspberry Pi
What to do if you get Swagger-codegen in python and Import Error: No module named
What to do if you get a Cannot retrieve metalink for repository error in yum
What to do if you get an error when running "certbot renew" in CakePHP environment
[Python] Get the update date of a news article from HTML
What to do if you get a UnicodeDecodeError with pip install
What to do if you can't use the trash in Lubuntu 18.04.
What to do when you get "I can't see the site !!!!"
What to do when the value type is ambiguous in Python?
What to do if you get angry with'vertices' must be a 2D list ... in matplotlib arrow
[Django] What to do if the model you want to create has a large number of fields
What to do if there is a decimal in python json .dumps
If you want a singleton in python, think of the module as a singleton
What to do if you can't use scikit grid search in Python
What to do if you get lost in file reference with FileNotFoundError
What to do if you get angry in TensorFlow v2 without attribute'app'
What to do if you get a TypeError with numpy min, max
What to do if you get an error when trying to load mnist
Get a datetime instance at any time of the day in Python
What happens if you do "import A, B as C" in Python?
What to do if you get an error when installing Dlib (Ubuntu)
What to do if you get "The session could not be opened" when installing CentOS on VirtualBox
What to do if you get angry with "Value Error: unknown local: UTF-8" in python manage.py syncdb
What to do if you get a Permission denied (public key) error when trying to pull on Github
What to do if you get the error Target WSGI script'/var/www/xxx/xxx.wsgi' cannot be loaded as python module
Get the caller of a function in Python
What to do to get google spreadsheet in python
[Python] Get the day of the week (English & Japanese)
[python] What to do when an error occurs in send_keys of headless chrome
What to do when a warning appears around Python integration in Neovim's CheckHealth
What to do if a Unicode Encode Error occurs in Sublime Text Python
What to do if a version error occurs in the selenium Chrome driver
How to get a list of files in the same directory with python
What to do when you want to receive files from a Windows client remotely
What to do if you get the error Input contains NaN, infinity or a value too large for dtype ('float64'). In machine learning
What to do if you get an "unknown service" error from your gRPC server
How to get the number of digits in Python
What to do if the Microsoft Store opens even if you run python on Windows
Get the value of a specific key in a list from the dictionary type in the list with Python