Quantcast
Channel: MobileRead Forums - Kobo Developer's Corner
Viewing all articles
Browse latest Browse all 1561

[Q] Python Unicodeencodeerror in Kobo device

$
0
0
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?

Viewing all articles
Browse latest Browse all 1561

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>