python - 从7z文件中提取csv文件

10 浏览
0 Comments

python - 从7z文件中提取csv文件

我有许多包含在不同7z文件中的csv文件。我想在这些7z文件中找到特定的csv文件,并将它们解压到不同的目录中保存。\n我尝试了以下代码:\n

import os
import py7zlib
tree = r'存储7z文件的路径'
dst = r'存储csv文件的目标路径'
for dirpath, dirname, filename in os.walk(tree):
    for myfile in filename:
        if myfile.endswith('2008-01-01_2008-04-30_1.7z'):
            myZip = py7zlib.Archive7z(open(os.path.join(dirpath,myfile), 'rb'))
            csvInZipFile = zip(myZip.filenames,myZip.files)
            for myCsvFileName, myCsvFile in csvInZipFile:
                if '2008-01' in myCsvFileName:
                    with open(os.path.join(dst,myCsvFileName),'wb') as outfile:
                        outfile.write(myCsvFile.read())

\n但是我遇到了以下错误:\n

Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Users\'\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 682, in runfile
execfile(filename, namespace)
  File "C:\Users\'\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 85, in execfile
    exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
  File "C:/Users//'/Documents/Example/unzipfiles.py", line 23, in 
outfile.write(myCsvFile.read())
  File "C:\Users\'\Anaconda3\lib\site-packages\py7zlib.py", line 576, in read
    data = getattr(self, decoder)(coder, data)
  File "C:\Users\'\Anaconda3\lib\site-packages\py7zlib.py", line 634, in _read_lzma
return self._read_from_decompressor(coder, dec, input, checkremaining=True, with_cache=True)
  File "C:\Users\'\Anaconda3\lib\site-packages\py7zlib.py", line 611, in _read_from_decompressor
    tmp = decompressor.decompress(data)
ValueError: data error during decompression

\n奇怪的是,这个方法似乎对前两个csv文件正常工作。我不知道如何找到问题的根源。至少csv文件中的数据似乎没有什么不同。手动使用IZArc解压不同的csv文件没有问题。(该问题在python 2.7和3.4中都出现过)。\n我还尝试使用lzma模块,但是我无法找出如何提取7z文件中包含的不同csv文件。

0