0

I have list of strings - or better 90% are literal stings But sometimes there is also octal encoded string

aL = ['literalString, XYZ','Verri\303\250res-le-buisson, IDF'] I am retrieveing list items from input (json file)

I need to convert it to utf-8

This works as intended:

x = b'Verri\303\250res-le-buisson, IDF'
print(f'{type(x)}')
print(x.decode('utf-8'))

<class 'bytes'>
Verrières-le-buisson, IDF

The problem is that I cannot find the way to prefix (use functionality) of 'b' to variable.

As I would need:

b'x.decode('utf-8')

Converting variable to bytes does not work properly (like bytes(x,'utf-8')).

Viki-Y
  • 33
  • 5
  • Does this answer your question? https://stackoverflow.com/a/46650050/12479639 – Axe319 Sep 15 '20 at 18:33
  • In short: `x = 'Verri\303\250res-le-buisson, IDF'` `print(x.encode('latin1').decode('unicode-escape').encode('latin1').decode('utf-8'))` – Axe319 Sep 15 '20 at 18:35
  • @Axe319 thanks a lot, yes now I understand better and it works – Viki-Y Sep 15 '20 at 19:22

0 Answers0