If you don't know a
's encoding, you'll need to:
- detect
a
's encoding - encode
b
using the detected encoding
First, to detect a
's encoding, let's use chardet.
$ pip install chardet
Now let's use it:
>>> import chardet
>>> a = '汉'
>>> chardet.detect(a)
{'confidence': 0.505, 'encoding': 'utf-8'}
So, to actually accomplish what you requested:
>>> encoding = chardet.detect(a)['encoding']
>>> b = u'汉'
>>> b_encoded = b.encode(encoding)
>>> a == b_encoded
True