0
votes

So i have a problem that script doesn't add Cyrillic symbols to list, i'm getting this error:

Traceback (most recent call last): File "c:\Users\Viktor\Desktop\Folder Compare.py", line 23, in print(list_script) File "C:\Users\Viktor\AppData\Local\Programs\Python\Python36-32\lib\encodings\cp1252.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 'charmap' codec can't encode characters in position 6-11: character maps to

I found couple solutions on stack overflow, but they didn't work for me well

list1, list2 = ([] for i in range(2))

for dirpath, dirnames, filenames in os.walk('D:\\Folder'):
    for fname in filenames:
        list1.append(fname)


for list_script in list1:
    print(list_script)
2
I do not think this is the problem. Please provide full error and in particular the line where error does occur. - Elis Byberi
@ElisByberi added full error message - Andrew
UnicodeEncodeError It is not valid Unicode string. - Elis Byberi
So the error is on the print line: the Cyrillic symbol has been successfully added, but your system cannot display it. You should say more about your system: python version (2 or 3), os, and default character set - Serge Ballesta
python 3.6, Windows 10, utf-8 - Andrew

2 Answers

2
votes

The problem is that where you are printing has cp1252 encoding. This encoding cannot represent cryllic characters, so it throws an error when you try to print them.

Try to change your terminal encoding.

1
votes

You could try set PYTHONIOENCODING enviroment variable to 'UTF-8':

os.environ["PYTHONIOENCODING"] = 'UTF-8'

or change stdout encoding:

sys.stdout = codecs.getwriter('utf8')(sys.stdout.buffer)