Python - Transfer string without escape or with encoding? - python

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'))

Related

Why am I not getting any results with print and for?

I ran this code and for some reason am just getting blank output. Thought it was because it had looped through it already and I just missed it, and there isn't anything left to print but this happens even for a new file. Super new to Python so any help would be appreciated! Unrelated, is there a way to remove the >> lines in Visual Studio so I can see the output more clearly?
>>>buffet = ('rice','salad','beets','oranges','cake')
>>>
>>>print(buffet)
>>>
>>>#menu rewritten
>>>
>>>buffet = ('rice','salad','beets','ice cream','fudge')
>>>
>>>print("\nModified dimensions:")
>>>
>>>for buffetitem in buffet:
print(buffetitem)
My output:
>>> buffet = ('rice','salad','beets','oranges','cake')
>>>
>>> print(buffet)
('rice', 'salad', 'beets', 'oranges', 'cake')
>>> #menu rewritten
>>>
>>> buffet = ('rice','salad','beets','ice cream','fudge')
>>> print("\nModified dimensions:")
Modified dimensions:
>>>
>>> for buffetitem in buffet:
... print(buffetitem)
...
You should press enter until you see the >>> and then enter again. Then it should run.

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

converting binary to utf-8 in 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

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.

Python. The coding of a string

>>> word = '\u041a\u041e\u041b'
>>> print u'\u041a\u041e\u041b'
КОЛ
>>> print word
\u041a\u041e\u041b
How to transform string as a variable to a
readable kind (how print word)?
>>> print '\u041a\u041e\u041b'.decode('unicode-escape')
КОЛ

Categories