Heavenly Raven
Heavenly Raven

Reputation:

UTF-8 is not working with Cyrillic alphabet in Pycharm

It works okay if I'm doing something within a program, but it doesn't work when I'm reading a file.

with open('test.txt', 'r') as f:

    print(f.read())

Input (text.txt):

слово
строка

Output:

слово
строка

I set both global and project encoding to UTF-8. No result.

Upvotes: 1

Views: 1574

Answers (1)

Mark Tolonen
Mark Tolonen

Reputation: 177610

The file is encoded in UTF-8, but your locale default is cp1251. Be explicit and always open a file with its known encoding:

#!python3
with open('test.txt', encoding='utf8') as f:
    print(f.read())

Python 2 users need to use the io module. The built-in open of Python 2 doesn't support the encoding parameter. io.open is Python 3's implementation and is available in Python 2 and Python 3 for portability.

This code is compatible with both Python 2 and 3:

from __future__ import print_function # for Python 3 print syntax in Python 2.
import io
with io.open('test.txt', encoding='utf8') as f:
    print(f.read())

Ref: open

Upvotes: 2

Related Questions