converting binary to utf-8 in python - python

I have a binary like this:
1101100110000110110110011000001011011000101001111101100010101000
and I want to convert it to utf-8.
how can I do this in python?

Cleaner version:
>>> test_string = '1101100110000110110110011000001011011000101001111101100010101000'
>>> print ('%x' % int(test_string, 2)).decode('hex').decode('utf-8')
نقاب
Inverse (from #Robᵩ's comment):
>>> '{:b}'.format(int(u'نقاب'.encode('utf-8').encode('hex'), 16))
1: '1101100110000110110110011000001011011000101001111101100010101000'

Well, the idea I have is:
1. Split the string into octets
2. Convert the octet to hexadecimal using int and later chr
3. Join them and decode the utf-8 string into Unicode
This code works for me, but I'm not sure what does it print because I don't have utf-8 in my console (Windows :P ).
s = '1101100110000110110110011000001011011000101001111101100010101000'
u = "".join([chr(int(x,2)) for x in [s[i:i+8]
for i in range(0,len(s), 8)
]
])
d = u.decode('utf-8')
Hope this helps!

>>> s='1101100110000110110110011000001011011000101001111101100010101000'
>>> print (''.join([chr(int(x,2)) for x in re.split('(........)', s) if x ])).decode('utf-8')
نقاب
>>>
Or, the inverse:
>>> s=u'نقاب'
>>> ''.join(['{:b}'.format(ord(x)) for x in s.encode('utf-8')])
'1101100110000110110110011000001011011000101001111101100010101000'
>>>

Use:
def bin2text(s): return "".join([chr(int(s[i:i+8],2)) for i in xrange(0,len(s),8)])
>>> print bin2text("01110100011001010111001101110100")
>>> test

Related

Python - Transfer string without escape or with encoding?

Python 2.7.10 Shell
>>> a = "\\xe4\\xbb\\xa5\\xe5\\x8f\\xa5\\xe3\\x81\\x82\\xe3\\x81\\xae"
>>> b = "\xe4\xbb\xa5\xe5\x8f\xa5\xe3\x81\x82\xe3\x81\xae"
>>> print a
\xe4\xbb\xa5\xe5\x8f\xa5\xe3\x81\x82\xe3\x81\xae
>>> print b
以句あの
>>>
Var a is exactly same as var b in our eyes, but they are different in bytes/bits level. Now I want the print-result of a is same as the print-result of b, any solutions?
In other word, how to transfer a to b ?
Thanks in advance :)
Thanks to #Bishakh Ghosh 's answer, help me a lot.
In the specific version of my Python:
>>> print a.decode('string-escape')
以句あの
>>> print a.decode('unicode_escape')
以å¥ãã
>>> b = a.decode('string-escape')
Thanks ~~~ ((●'◡'●)ノ♥
This should do the trick:
b = a.decode('string-escape')
Or if you want to print a directly:
print(a.decode('string-escape'))

Checking two string in python?

let two strings
s='chayote'
d='aceihkjouty'
the characters in string s is present in d Is there any built-in python function to accomplish this ?
Thanks In advance
Using sets:
>>> set("chayote").issubset("aceihkjouty")
True
Or, equivalently:
>>> set("chayote") <= set("aceihkjouty")
True
I believe you are looking for all and a generator expression:
>>> s='chayote'
>>> d='aceihkjouty'
>>> all(x in d for x in s)
True
>>>
The code will return True if all characters in string s can be found in string d.
Also, if string s contains duplicate characters, it would be more efficient to make it a set using set:
>>> s='chayote'
>>> d='aceihkjouty'
>>> all(x in d for x in set(s))
True
>>>
Try this
for i in s:
if i in d:
print i

Python add a string to all set's elements

I'm wondering what is the Python way to perform the following -
Given a set :
s = {'s1','s2','s3'}
I would like to perform something like :
s.addToAll('!')
to get
{'s1!','s2!','s3!'}
Thanks!
For an actual set:
>>> s = {'s1','s2','s3'}
>>> {x + '!' for x in s}
set(['s1!', 's2!', 's3!'])
That method is 2.7+, If you are using Python 2.6 you would have to do this instead:
>>> s = set(['s1','s2','s3'])
>>> set(x + '!' for x in s)
set(['s1!', 's2!', 's3!'])
You can try this:
>>> s = ['s1','s2','s3']
>>> list(i + '!' for i in s)

Python error: could not convert string to float

I have some Python code that pulls strings out of a text file:
[2.467188005806714e-05, 0.18664554919828535, 0.5026880460053854, ....]
Python code:
v = string[string.index('['):].split(',')
for elem in v:
new_list.append(float(elem))
This gives an error:
ValueError: could not convert string to float: [2.974717463860223e-06
Why can't [2.974717463860223e-06 be converted to a float?
You've still got the [ in front of your "float" which prevents parsing.
Why not use a proper module for that? For example:
>>> a = "[2.467188005806714e-05, 0.18664554919828535, 0.5026880460053854]"
>>> import json
>>> b = json.loads(a)
>>> b
[2.467188005806714e-05, 0.18664554919828535, 0.5026880460053854]
or
>>> import ast
>>> b = ast.literal_eval(a)
>>> b
[2.467188005806714e-05, 0.18664554919828535, 0.5026880460053854]
You may do the following to convert your string that you read from your file to a list of float
>>> instr="[2.467188005806714e-05, 0.18664554919828535, 0.5026880460053854]"
>>> [float(e) for e in instr.strip("[] \n").split(",")]
[2.467188005806714e-05, 0.18664554919828535, 0.5026880460053854]
The reason your code is failing is, you are not stripping of the '[' from the string.
You are capturing the first bracket, change string.index("[") to string.index("[") + 1
This will give you a list of floats without the need for extra imports etc.
s = '[2.467188005806714e-05, 0.18664554919828535, 0.5026880460053854]'
s = s[1:-1]
float_list = [float(n) for n in s.split(',')]
[2.467188005806714e-05, 0.18664554919828535, 0.5026880460053854]
v = string[string.index('[') + 1:].split(',')
index() return index of given character, so that '[' is included in sequence returned by [:].

how to get this string using python

i have a list like this :
a=[1000,200,30]
and i want to get a list like this :
['01000','00200','00030']
so what can i do ,
thanks
>>> a=[1000,200,30]
>>> [str(e).zfill(5) for e in a]
['01000', '00200', '00030']
str.zfill
str.format() is the preferred way to do this if you are using Python >=2.6
>>> a=[1000, 200, 30]
>>> map("{0:05}".format, a)
['01000', '00200', '00030']
You can do it like this:
a = [1000,200,30]
b = ["%05d" % (i) for i in a]
print b
The number tells the width and the leading zero says that you want leading zeros.
map(lambda x:str(x).zfill(5),a)
Look at formatting strings in Python.

Categories