在二进制读取模式(解码错误)中打开luigi.localtarget



我正在尝试打开一个luigi.localtarget,以读取指向zip文件(以便我可以计算一个哈希(。不幸的是,当我尝试阅读它时,我会得到一个UnicodeDecodeError,我认为这意味着它不会以二进制文件打开。

我可以做到这一点(没有luigi(,它可以正常工作

file_path  = luigi.LocalTarget('myfile.zip')
with open(file_path, 'rb') as f:
    data = f.read(1048576)

,但是如果我这样做

target = luigi.LocalTarget(file_path)
with target.open('rb') as f:
    data = f.read(1048576)

我得到了这个

---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-28-5240759ed677> in <module>()
      1 target = luigi.LocalTarget(file_path)
      2 with target.open('rb') as f:
----> 3     data = f.read(1048576)
/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/codecs.py in decode(self, input, final)
    319         # decode input (taking the buffer into account)
    320         data = self.buffer + input
--> 321         (result, consumed) = self._buffer_decode(data, self.errors, final)
    322         # keep undecoded input until the next call
    323         self.buffer = data[consumed:]
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 10: invalid continuation byte

我正在使用Python 3.6和Luigi 2.6.1。预先感谢您的任何帮助

luigi在开放的方法中剥离'b'和't',不确定为什么:

http://luigi.readthedocs.io/en/stable/_modules/luigi/local_target.html#localtarget.open

def open(self, mode='r'):
        rwmode = mode.replace('b', '').replace('t', '')

最新更新