[英]UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
I am saving a list using pickle.dumps()
as so: 我正在使用
pickle.dumps()
保存列表,如下所示:
my_list = ['Hello', 'I', 'Have', 'a', 'question', 'camión']
my_pickle = pickle.dumps(my_list)
Once I have the pickle created I am uploading it to a container in Azure Batch: 创建泡菜后,我将其上载到Azure Batch中的容器中:
blob_service.block_service.create_blob_from_bytes('containername', 'filename', my_pickle)
And getting it back: 并将其取回:
my_bytes = blob_service.block_service.get_blob_to_bytes('containername', 'filename')
What I want ( my_list
) is inside my_bytes.content
and, if I printed I get: 我想要的(
my_list
)位于my_bytes.content
内,如果我打印出来,我会得到:
b'\\x80\\x03]q\\x00(X\\x05\\x00\\x00\\x00Helloq\\x01X\\x01\\x00\\x00\\x00Iq\\x02X\\x04\\x00\\x00\\x00Haveq\\x03X\\x01\\x00\\x00\\x00aq\\x04X\\x08\\x00\\x00\\x00questionq\\x05X\\x07\\x00\\x00\\x00cami\\xc3\\xb3nq\\x06e.'
To get my_bytes
back to the list, I tried to decode it as follows: 为了使
my_bytes
返回列表,我尝试对其进行解码,如下所示:
my_bytes.decode('utf-8')
But I am getting the following error: 但是我收到以下错误:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
Other encoding, as latin-1
, it is not giving mistake but it is returning 其他编码(如
latin-1
)没有给出错误,但正在返回
'\\x80\\x03]q\\x00(X\\x05\\x00\\x00\\x00Helloq\\x01X\\x01\\x00\\x00\\x00Iq\\x02X\\x04\\x00\\x00\\x00Haveq\\x03X\\x01\\x00\\x00\\x00aq\\x04X\\x08\\x00\\x00\\x00questionq\\x05X\\x07\\x00\\x00\\x00camiónq\\x06e.'
rather than my_list
. 而不是
my_list
。
I have searched on Internet to find out why this is happening but I couldn't find anything that helped me, so any suggestion or advice is more than welcome. 我在Internet上进行了搜索,以了解为什么会发生这种情况,但找不到任何对我有帮助的东西,因此,任何建议或建议都非常受欢迎。 I would like to get
my_list
from my_bytes
. 我想从
my_bytes
获取my_list
。 Please note that I am interested on how to decode my_bytes
. 请注意,我对如何解码
my_bytes
。
Use pickle.loads
to reverse pickle.dumps
: 使用
pickle.loads
反转pickle.dumps
:
>>> s = b'\x80\x03]q\x00(X\x05\x00\x00\x00Helloq\x01X\x01\x00\x00\x00Iq\x02X\x04\x00\x00\x00Haveq\x03X\x01\x00\x00\x00aq\x04X\x08\x00\x00\x00questionq\x05X\x07\x00\x00\x00cami\xc3\xb3nq\x06e.'
>>> import pickle
>>> pickle.loads(s)
['Hello', 'I', 'Have', 'a', 'question', 'camión']
What I finally did was converting my list to string as: 我最后要做的就是将列表转换为字符串:
my_list=str(my_list)
And upload it to Blob Storage as: 并将其上传到Blob存储为:
BlockStorage('<account_name>', '<account_key>').block_service.create_blob_from_text('<container_name>', '<file_name>', my_list)
And finally, to download it: 最后,下载它:
my_file_as_str = BlockStorage('<account_name>', '<account_key>').block_service.get_blob_to_text('<container_name>', '<file_name>')
To get back my list I just needed to do now: 要获取我的列表,我现在需要做的是:
my_list = eval(my_file_as_str.content)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.