View Single Post
Old 06-25-2020, 09:46 AM   #1
snoman
Junior Member
snoman began at the beginning.
 
Posts: 6
Karma: 10
Join Date: Jun 2020
Device: Kobo Aura One
[Q] Python Unicodeencodeerror in Kobo device

Hi, I'm a newbie here and kobo programming...

I installed Python 3.4 with reference to Lucas Malor's post(https://www.mobileread.com/forums/sh....php?p=2885820)

And wrote some python scripts, but met exceptions while handling non-alphabetical characters - Korean.
My simple code is below.
Quote:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import os
import sys

path = '/mnt/onboard/Library'
for sub in os.listdir(path):
print(sub)
Result:
Quote:
<_io.TextIOWrapper name='<stderr>' mode='w' encoding='ANSI_X3.4-1968'> <_io.TextIOWrapper name='<stdout>' mode='w' encoding='ANSI_X3.4-1968'>
...
Traceback (most recent call last):
File "./ta.py", line 11, in <module>
print(sub)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 16-27: ordinal not in range(128)
Alphabet-named-files are printed normally, but Non-alphabet-named-files occurred exceptions.

So, I tried to set PYTHONIOENCODING,

Quote:
export PYTHONIOENCODING="UTF-8"
Result:
Quote:
<_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'> <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
...
Traceback (most recent call last):
File "./ta.py", line 11, in <module>
print(sub)
UnicodeEncodeError: 'utf-8' codec can't encode character '\udceb' in position 16: surrogates not allowed
Ofcourse, I can use decode() or encode(), but in this case, there will be other problems like save or web-requesting non-alphabetic-named files.

What can I do to solve this problem?
snoman is offline   Reply With Quote