2

I'm using python3 in pycharm but it seems that pycharm doesn't take utf8 as default encoding method.

For example, I try to open with out setting encoding parameter and print a line

with open('/Users/test.txt','r') as file:
    print(file.readline())

and then I got

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 0: ordinal not in range(128)

While I can open the file without setting the 'encoding' parameter in iterm2 and Jupyter notebook. So I guess it is due to the encoding setting in pycharm, and I have tried every encoding setting in pycharm it also didn't work.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Henry
  • 23
  • 2
  • 4

2 Answers2

3

PyCharm passes a blank value for the LC_CTYPE environment variable:

Please see: PyCharm is changing the default encoding in my Django app

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
zh bo
  • 31
  • 2
2

I've tested on python-IDLE while not on pycharm, and I got the same response as yours. The encoding of file is UTF-8, while the encoding method used open the file is ASCII. I've found 2 solutions to avoid this:

Change the encoding method used to open the file:

The codec module contains some operation to do this. For convenience, you can directly pass a encoding parameter in your code to declare the encoding method.

with open('/Users/test.txt','r',encoding="utf-8") as file:
    print(file.readline())

Change the encoding method of the file:

There are many ways to change the encoding method of a file. If you are using Windows OS, that will be more convenient. Open the test.txt file using Notepad, then click File > Save as.... In the Save as interface, change the encoding of file(at the bottom position) from UTF-8 to ANSI, finally Save and replace the original file. After doing this, try to run the code in either pycharm or other IDE, and you will get correct result. Hope it helps.

WW00WW
  • 417
  • 4
  • 15
  • Thanks for answering this question, I can **open** with setting the **encoding=utf-8** parameter explicitly, but I just confused about why the default setting is not 'utf-8' in pycharm. what's more when I try to write the logging information into file, it also raises the same error and I have to manually set the encoding method with **logging.FileHandler**. – Henry Jan 26 '18 at 03:53
  • Anyway, this problem may be trivial and there may be not many people meet the same question, it just needs me to set the **encoding** parameter explicitly every time. – Henry Jan 26 '18 at 04:04
  • Hmmm, the default file encoding in pycharm is UTF-8 on my computer. Maybe you can reset it to UTF-8 and try that code again? – WW00WW Jan 26 '18 at 04:47