以二进制读取模式打开luigi.LocalTarget(解码错误)

时间:2017-05-03 15:47:21

标签: python python-3.x luigi

我试图打开一个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。在此先感谢您的任何帮助

1 个答案:

答案 0 :(得分: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', '')