[Windows] The problem that an error occurs when opening a file other than CP932 (Shift-JIS) encoded in Python has been solved for the time being.

When I try to open a file containing Japanese using the Python ʻopen ()` function in a Windous environment, the following error occurs.

UnicodeDecodeError: 'cp932' codec can't decode byte 0xef in position 60: illegal multibyte sequence

In the article here that I referred to, I taught you how to avoid the error by adding the encoding to the program. However, it's hard to add encoding everywhere you use the Python's ʻopen () function. It's painful to add encoding everywhere you use the ʻopen () function every time you install a package. So, I investigated whether ʻUTF-8 encoding can be specified collectively in the ʻopen ()function ofPython. As a result, it was found that it can be supported from the following versions of Windows 10`, so the procedure is described.

version

We have confirmed that it works with the following OS versions. Version: If it is 1709 or earlier, the target function is not implemented, so the following procedure cannot be performed. Version: The version between 1709 and 1809 has not been verified. バージョン.png

procedure

  1. Open the control panel and press the Region link. コントロールパネル.png

  2. Press the Change System Localization button on the Administration tab. 地域.png

  3. Check Use Unicode UTF-8 with worldwide language support and press the ʻOK` button. システムローケル.png

  4. You will be asked to restart the system, so restart it.

result

I was able to check files containing Japanese using the package cfn-lint in a Windows 10 environment.

$ cfn-lint test.yaml
E0000 did not find expected key
test.yaml:2:3

in conclusion

Although it is a feature of worldwide language support, it is a beta feature as shown in the screen capture, so using it may affect other features. At the time of writing this article, we have not verified all areas of influence. Those who use it are at their own risk.

Recommended Posts

[Windows] The problem that an error occurs when opening a file other than CP932 (Shift-JIS) encoded in Python has been solved for the time being.
Workaround for the problem that sys.argv is not passed when executing a Python script with only the file name in Python2.7 on Windows
Precautions when opening other than CP932 (Shift-JIS) encoding on Windows
A memo when creating an environment that can be debugged with Lambda @ Edge for the time being
A useful note when using Python for the first time in a while
[Python] I want to know the variables in the function when an error occurs!
Create an exe file that works in a Windows environment without Python with PyInstaller
Check the argument type annotation when executing a function in Python and make an error
I got an SSL Error when I installed Anaconda in a new environment, so I solved it (Windows10, Anaconda3-2019.10)
Solved the problem that sqlite3 got undefined error when cross-compiling Golang
A function that measures the processing time of a method in python
Get the formula in an excel file as a string in Python
Try using FireBase Cloud Firestore in Python for the time being
An error that does not work as expected when calling the tkinter module in a text editor
I get an error when I put a Python plugin in Visual Studio Code under the pyenv environment