Python EncodeDecode error: UnicodeDecodeError: 'charmap' codec can't decode byte [duplicate]

怎甘沉沦 提交于 2021-01-29 09:29:09

问题


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>

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


回答1:


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

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

This is an example...

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


来源:https://stackoverflow.com/questions/56099686/python-encodedecode-error-unicodedecodeerror-charmap-codec-cant-decode-byte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!