【问题标题】:UnicodeDecodeError reading binary inputUnicodeDecodeError 读取二进制输入
【发布时间】:2021-09-17 00:58:01
【问题描述】:

我尝试将我的代码从 python 2.7 移植到 3.4。在 2.7 下它工作得很好。 我将导入一个 Picoscope 二进制文件,在 Python 3.4 (ubuntu) 下得到如下错误:

in __import_CS
data_A = bin_file_A.read(64)
File "/usr/lib/python3.4/codecs.py", line 313, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf3 in position 1: invalid continuation byte

这里是代码片段:

bin_file_A = open(infile ,"r")
bin_file_B = open(infile_B ,"r")

######  read file header ########
data_A = bin_file_A.read(64)
data_B = bin_file_B.read(64)

start = 0
stop = struct.calcsize('8d')
n_A = struct.unpack('>8d', data_A[start:stop])
n_B = struct.unpack('>8d', data_B[start:stop])

【问题讨论】:

    标签: python linux python-3.x


    【解决方案1】:

    需要以二进制方式读取文件:

    bin_file_A = open(infile_A, "rb")
    bin_file_B = open(infile_B, "rb")
    

    【讨论】:

      猜你喜欢
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 2014-10-15
      • 2011-02-20
      • 2015-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多