简体   繁体   English

UnicodeDecodeError:'utf-8'编解码器无法解码位置0的字节0x80:无效的起始字节

[英]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.

相关问题 Python:UnicodeDecodeError:'utf-8'编解码器无法解码 position 中的字节 0x80 0:无效起始字节 - Python: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte UnicodeDecodeError: &#39;utf-8&#39; 编解码器无法解码位置 3131 中的字节 0x80:起始字节无效 - UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 3131: invalid start byte UnicodeDecodeError:&#39;utf-8&#39;编解码器无法解码位置3131中的字节0x80:我的代码中的无效起始字节 - UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 3131: invalid start byte in my code 在Windows上使用python错误:UnicodeDecodeError:&#39;utf-8&#39;编解码器无法解码位置110的字节0x80:无效的起始字节 - using python on windows error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 110: invalid start byte UnicodeDecodeError:&#39;utf8&#39;编解码器无法解码位置11的字节0x80:无效的起始字节 - UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 11: invalid start byte 'utf-8' 编解码器无法解码 position 中的字节 0x80 28:起始字节无效 - 'utf-8' codec can't decode byte 0x80 in position 28: invalid start byte Python UnicodeDecodeError:&#39;utf8&#39;编解码器无法解码位置74的字节0x80:无效的起始字节 - Python UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 74: invalid start byte UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 3131: invalid start byte - 有人可以帮我吗? - UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 3131: invalid start byte - Some one can help me? 使用Pycrypto Python进行AES解密异常:&#39;builtins.UnicodeDecodeError:&#39;utf-8&#39;编解码器无法解码位置0的字节0x80:无效的起始字节&#39; - AES Decryption using Pycrypto Python Exception : 'builtins.UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte' 'utf-8' 编解码器无法解码 position 中的字节 0x80 3131:无效的起始字节':在读取 xml 文件时 - 'utf-8' codec can't decode byte 0x80 in position 3131: invalid start byte': while reading xml files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM