I am using python3 to call a function written in another language. The function returns a string which should be a Chinese character. But I only get a string which is b'\xbb\xf9\xb2\xee', not the Chinese character I am expecting. How do I convert it back to the character I wanted? Thanks.
Update: the code is
temp=ts.RemoteCallFunc("StockIndexFutures_basis",["IF00","SH000300",ts.EncodeDate(2012,3,7),ts.EncodeDate(2012,5,1),"日线"],{});
and the result is a list :
[{b'\xbb\xf9\xb2\xee': 7.313000000000102, b'SH000300': 2631.487, b'date': b'2012-04-26', b'IF00': 2638.8}, {b'\xbb\xf9\xb2\xee': 12.442999999999756, b'SH000300': 2626.157, b'date': b'2012-04-27', b'IF00': 2638.6}]
-
Which encoding do you except your text to be? It doesn't look like UTF-8.Klaus D.– Klaus D.2016年03月09日 08:58:14 +00:00Commented Mar 9, 2016 at 8:58
1 Answer 1
If you want to convert the byte string b'\xbb\xf9\xb2\xee' to the proper characters, you can call decode on the byte string and specify one of standard codecs. For example:
x = b'\xbb\xf9\xb2\xee'
x.decode('gbk') # gbk is listed as the "unified Chinese" codec
'基差'
I'm just guessing on the codec and you may want to experiment with that.
It's not clear which Python 3.x version you're using, so I tested this with Python 3.1, 3.2, 3.3, and 3.4 and the results are the same for all versions.