cone001
cone001

Reputation: 1153

python string encoding error

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}]

Upvotes: 0

Views: 67

Answers (1)

larsbutler
larsbutler

Reputation: 533

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.

Upvotes: 2

Related Questions