Python EncodeDecode错误:UnicodeDecodeError:'charmap'编解码器无法解码字节 [英] Python EncodeDecode error: UnicodeDecodeError: 'charmap' codec can't decode byte

查看:136
本文介绍了Python EncodeDecode错误:UnicodeDecodeError:'charmap'编解码器无法解码字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理图像,但无法摆脱该错误:

I'm trying to manipulate images but I can't get rid of that error :

fichier=open("photo.jpg","r")

lignes=fichier.readlines()
Traceback (most recent call last):

  File "<ipython-input-32-87422df77ac2>", line 1, in <module>
    lignes=fichier.readlines()

  File "C:\Winpython\python-3.5.4.amd64\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]

UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 207: character maps to <undefined>

我见过一些论坛,人们说要在"open ..."中添加encoding ='utf-8',但这是行不通的

I've seen forums where people say to add encoding='utf-8' in the "open ..." but that won't work

推荐答案

您的问题出在 open()命令上.您的jpeg图片是二进制文件,应使用 open('photo.jpg','rb').

Your problem is with the open() command. Your jpeg image is binary and you should use open('photo.jpg', 'rb').

也不要为此文件使用 readlines();此功能应用于字符输入.

Also do not use readlines() for this file; this function should be used for character inputs.

这是一个例子...

import struct

with open('photo.jpg', 'rb') as fh:
    raw = fh.read()
for ii in range(0, len(raw), 4):
    bytes = struct.unpack('i', raw[ii:ii+4])  
    # do something here with your data

这篇关于Python EncodeDecode错误:UnicodeDecodeError:'charmap'编解码器无法解码字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆