Error running Python program: UnicodeDecodeError

Asked 2 years ago, Updated 2 years ago, 298 views

When I run the following program from Atom, I get an error.Please let me know

error:

Traceback (most recent call last):
  File "C:\Users\Katsu\Desktop\python\python.py", line 6, in <module>
    data=f.read()
UnicodeDecodeError: 'cp932' codec can't decode byte 0x81 in position21: illegal multibyte sequence

program:

import re

source='english_words_01.txt'

with open(source) asf:
    data=f.read()

print(data)

python python3

2022-09-30 21:55

1 Answers

I guess this is it.
The Python program in Windows cannot read UTF-8 text files

Python text file input/output on Windows is set to AnsiCodePage (shift JIS=cp932) by default.
The article around here also shows how to deal with situations and environmental variables.
Default UTF-8 on Python on Windows
Unicode HOWTO

However, encoding will still be mixed, so encoding for each file should be specified.

Also note that using common features such as python.py or test or the name of the module you are trying to load can cause problems.It's good to search and remember the common mistakes of beginners


2022-09-30 21:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.