Decoding strings from textfile

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Decoding strings from textfile



I have a text file that is reading in another application's script.
This how it looks like in notepad:


Одинцовская РЭС: М.О., Одинцовский район
Климовская РЭС: М.О., г.о. Подольск
Кульшовская РЭС: М.О., г.о. Подольск



This file is needed for two things:
1. Creating a dict of values, separated by ':'. I use this dict in another part of script
2. Allows user to select desirable value



enter image description here



Here is what user see when he launhes script.
When a certain value is selected I have to use it in dictionary. But the problem is that selection is in unicode format (because of features of script building in ArcGIS) while the dictionary's keys are str.
So I need a value in dictionary which looks like
'xcexe4xe8xedxf6xeexe2xf1xeaxe0xff xd0xddxd1' to be converted in unicode. But when I make .encode('utf-8') it throws an error


unicode


str


'xcexe4xe8xedxf6xeexe2xf1xeaxe0xff xd0xddxd1'


.encode('utf-8')



UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 0: ordinal not in range(128)


UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 0: ordinal not in range(128)




1 Answer
1



This should work


>>> c = b'xcexe4xe8xedxf6xeexe2xf1xeaxe0xff xd0xddxd1'
>>> c
b'xcexe4xe8xedxf6xeexe2xf1xeaxe0xff xd0xddxd1'
>>> c.decode('unicode_escape')
'Îäèíöîâñêàÿ ÐÝÑ'



The b'' prefix denotes sequence of 8-bit bytes.



Take a look at SO read russian characters





fine, that works! But is there a method for that, i.e. new_word = b(c)?
– Pavel Pereverzev
Aug 8 at 8:26


new_word = b(c)





Read this docs.python.org/3/howto/unicode.html
– Richard Rublev
Aug 8 at 8:37






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered