Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Readers > Kobo Reader > Kobo Developer's Corner

Notices

Reply
 
Thread Tools Search this Thread
Old 06-25-2020, 09:46 AM   #1
snoman
Junior Member
snoman began at the beginning.
 
Posts: 8
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
Old 06-25-2020, 12:02 PM   #2
snoman
Junior Member
snoman began at the beginning.
 
Posts: 8
Karma: 10
Join Date: Jun 2020
Device: Kobo Aura One
I forgot a line...

I forgot a line in my code, but I don't know how to edit original post

Quote:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import os
import sys

# Check the encoding of stderr/stdout.
print(sys.stderr, sys.stdout)

path = '/mnt/onboard/Library'
for sub in os.listdir(path):
print(sub)
snoman is offline   Reply With Quote
Advert
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Aura Install python on a kobo. SKK Kobo Developer's Corner 1 07-06-2019 06:21 PM
Python for Kobo Firmware 2.6+ KevinShort Kobo Developer's Corner 39 12-31-2017 01:38 PM
Python 3 (and Cython) on Kobo Lucas Malor Kobo Developer's Corner 17 08-21-2014 04:06 PM
Touch Python/Pygame successfully running on Kobo Touch KevinShort Kobo Developer's Corner 18 10-24-2012 03:08 PM
python command line tool for kobo reed Kobo Reader 2 06-05-2010 02:07 AM


All times are GMT -4. The time now is 03:23 AM.


MobileRead.com is a privately owned, operated and funded community.