2016-05-12 85 views
1

在python 3中,我有字符串u'\ xf4 \ xfb \ xe2'(unicode)。我需要將此字符串轉換爲字節b'\ xf4 \ xfb \ xe2'(即u'\ xf4' - > b'\ xf4'等)。 我可以使用Python中2得到這樣的結果:將錯誤的unicode字符串轉換爲字節

''.join([chr(ord(c)) for c in u'\xf4\xfb\xe2']) 

但是在Python 3 CHR()返回Unicode。 我如何在Python 3中做到這一點?

回答

2

試試這個:

bytes(map(ord, u'\xf4\xfb\xe2')) 
相關問題