Python Forum
How to convert unicode to koi8-r?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to convert unicode to koi8-r?
#2
(Feb-21-2019, 10:51 AM)AlekseyPython Wrote: When in Python I convert the English- Russian string to koi8-r, I get a strange sequence:
It's not strange you convert to bytes,in Python 3 are as string(text) Unicode bye default.
Moving to Python 3 Unicode was one the biggest changes.
>>> utf = 'My string, Моя строка'
>>> koi = utf.encode(encoding='koi8-r', errors='ignore')
>>> koi
b'My string, \xed\xcf\xd1 \xd3\xd4\xd2\xcf\xcb\xc1'

>>> koi.decode() # Default here is the same as koi.decode('utf-8')
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xed in position 11: invalid continuation byte

# Has to use same encoding to get it back to default text(Unicode) Python 3
# Should try to use utf-8 always,makes it better for everyone
>>> koi.decode('koi8-r')
'My string, Моя строка'
I don't see a reason why you should mess with this at all.
Try avoid encoding/decoding at all,let DB handle if needed.
If example test sqlite3 with Python 3.7.
There is no need to do anything,Python 3 text(Unicode default) will go in out DB without any problems.
E:\div_code\xy
λ ptpython -i dbb.py
>>> bid = Bidbase()
>>> bid.create_db()
 
>>> bid.insert('gleen mars', '345678')
>>> utf = 'My string, Моя строка'
>>> bid.insert('foo', utf)
 
>>> bid.read_all()
gleen mars : 345678
foo : My string, Моя строка
Reply


Messages In This Thread
RE: How to convert unicode to koi8-r? - by snippsat - Feb-21-2019, 04:18 PM
RE: How to convert unicode to koi8-r? - by DeaD_EyE - Mar-04-2019, 08:33 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert Int Value bigger 256 to Unicode lastyle 4 2,751 Mar-19-2020, 11:48 AM
Last Post: lastyle
  clean unicode string to contain only characters from some unicode blocks gmarcon 2 3,993 Nov-23-2018, 09:17 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020