2

I have a string encoded in utf8 and I want to convert it to 850 (encoding). I have done it with R like this but I don't know how to do it in Python: iconv(curve1,"UTF-8", "850", toRaw = TRUE)

Thank you!!

  • Welcome to Stack Overflow. Please take the 2-minute [tour]. Then, [edit] your question to provide a [mcve]. – JosefZ Jan 12 '21 at 14:37

2 Answers2

0

For python 3.9

#-*- coding:utf-8 -*-
import sys
a = 'This is a bit русские буквы.'
print ("sys.stdout.encoding",sys.stdout.encoding)
print('Original string:', a)
# Encoding in utf-8
encoded_bytes = a.encode('utf-8', 'replace')
print('Encoded string:', encoded_bytes)


decoded_cp1251 = encoded_bytes.decode('cp1251', 'replace')
decoded_cp866 = encoded_bytes.decode('cp866', 'replace')
decoded_utf8 = encoded_bytes.decode('utf-8', 'replace')

sys.stdout.reconfigure(encoding='cp1251')
print ("sys.stdout.encoding",sys.stdout.encoding)
print('Decoded string: 1251', decoded_cp1251)
sys.stdout.reconfigure(encoding='cp866')
print ("sys.stdout.encoding",sys.stdout.encoding)
print('Decoded string: 866', decoded_cp866)
sys.stdout.reconfigure(encoding='utf-8')
print ("sys.stdout.encoding",sys.stdout.encoding)
print('Decoded string: utf-8', decoded_utf8)

Output for example

PS C:\Users\Dmitry\PycharmProjects\python_sqlplus> python .\test_decode2.py
sys.stdout.encoding utf-8
Original string: This is a bit русские буквы.
Encoded string: b'This is a bit \xd1\x80\xd1\x83\xd1\x81\xd1\x81\xd0\xba\xd0\xb8\xd0\xb5 \xd0\xb1\xd1\x83\xd0\xba\xd0\xb2\xd1\x8b.'
sys.stdout.encoding cp1251
Decoded string: 1251 This is a bit русские буквы.
sys.stdout.encoding cp866
Decoded string: 866 This is a bit русские буквы.
sys.stdout.encoding utf-8
Decoded string: utf-8 This is a bit русские буквы.
PS C:\Users\Dmitry\PycharmProjects\python_sqlplus>
Dmitry Demin
  • 2,006
  • 2
  • 15
  • 18
  • the code you have provided raises the following error: 'OutStream' object has no attribute 'reconfigure'. I don't know if it's my fault but I have copied it. – Mireia Boneta Jan 13 '21 at 11:07
  • The code works for python 3.9 Since Python 3.7 you can change the encoding of standard streams with reconfigure(): https://stackoverflow.com/questions/4374455/how-to-set-sys-stdout-encoding-in-python-3 – Dmitry Demin Jan 13 '21 at 11:13
-1

Try this

string_utf = "your utf-8 encoded string"
string_cp850 = string_utf.encode(encoding='cp850')
ZealousWeb
  • 1,647
  • 1
  • 10
  • 11
  • I have already tried this function but it not gives the same result as the R code, that's why I was struggling with this task. Do you have any idea why it's not the same? – Mireia Boneta Jan 13 '21 at 11:08