我需要能够使用文件描述符打开压缩后的CSV文件。当我执行以下操作(这是仍会触发错误的简化代码)时:

file_path = r"E39O6KS6J8MIZW.00.0f6db4e5.gz"
fd = os.open(file_path, os.O_RDONLY, os.O_BINARY)
# ...
file_object = gzip.GzipFile(fileobj=io.FileIO(fd, mode='rb'))
reader = csv.reader(io.BufferedReader(file_object))
for row in reader:
    print row


我收到CRC检查错误:

Traceback (most recent call last):
  File "./log_processing_scripts/dev.py", line 40, in <module>
    post()
  File "./log_processing_scripts/dev.py", line 36, in post
    for row in reader:
  File "C:\Python27\lib\gzip.py", line 252, in read
    self._read(readsize)
  File "C:\Python27\lib\gzip.py", line 299, in _read
    self._read_eof()
  File "C:\Python27\lib\gzip.py", line 338, in _read_eof
    hex(self.crc)))
IOError: CRC check failed 0x4b77635f != 0xbe13716L


该文件未损坏(!),我可以使用gzip.open(file_path)对其进行处理。

我想念什么?

最佳答案

O_RDONLY和O_BINARY标志应该在一起,例如:

fd = os.open(file_path, os.O_RDONLY | os.O_BINARY)


参考:https://docs.python.org/2/library/os.html#open-constants

关于python - 带文件描述符的gzip,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33926537/

10-11 19:23
查看更多