从Zip文件中读取CSV

时间:2012-02-18 18:22:39

标签: python csv zip

我有一个zip文件目录(大约10,000个小文件),每个文件都是我试图阅读并分成许多不同CSV文件的CSV文件。

我设法编写代码以从CSV目录中拆分CSV文件,如下所示,它读取CSV的第一个属性,并根据它将其写入相关CSV。

import csv
import os
import sys
import re
import glob

reader = csv.reader(open("C:/Projects/test.csv", "rb"), delimiter=',', quotechar='"')
write10 = csv.writer(open('ouput10.csv', 'w'), delimiter=',', lineterminator='\n', quotechar='"', quoting=csv.QUOTE_NONNUMERIC)
write15 = csv.writer(open('ouput15.csv', 'w'), delimiter=',', lineterminator='\n', quotechar='"', quoting=csv.QUOTE_NONNUMERIC)


headings10=["RECORD_IDENTIFIER","CUSTODIAN_NAME","LOCAL_CUSTODIAN_NAME","PROCESS_DATE","VOLUME_NUMBER","ENTRY_DATE","TIME_STAMP","VERSION","FILE_TYPE"]
write10.writerow(headings10)

headings15=["RECORD_IDENTIFIER","CHANGE_TYPE","PRO_ORDER","USRN","STREET_DESCRIPTION","LOCALITY_NAME","TOWN_NAME","ADMINSTRATIVE_AREA","LANGUAGE"]
write15.writerow(headings15)


for row in reader:
    type = row[0]
    if "10" in type:        
        write10.writerow(row)
    elif "15" in type:
        write15.writerow(row)

所以我现在正在尝试阅读Zip文件而不是浪费时间首先提取它们。

这是我在跟踪了很多教程后到目前为止所得到的

import glob
import os
import csv
import zipfile
import StringIO

for name in glob.glob('C:/Projects/abase/*.zip'):
    base = os.path.basename(name)
    filename = os.path.splitext(base)[0]


datadirectory = 'C:/Projects/abase/'
dataFile = filename
archive = '.'.join([dataFile, 'zip'])
fullpath = ''.join([datadirectory, archive])
csv = '.'.join([dataFile, 'csv'])


filehandle = open(fullpath, 'rb')
zfile = zipfile.ZipFile(filehandle)
data = StringIO.StringIO(zfile.read(csv))
reader = csv.reader(data)

for row in reader:
    print row

然而,错误被抛出

AttributeError:'str'对象没有属性'reader'

希望有人可以告诉我如何更改用于读取Zip文件的CSV读取代码。

非常感谢

1 个答案:

答案 0 :(得分:19)

简单修复。您正在使用本地csv变量覆盖csv模块。只需更改该变量的名称:

import glob
import os
import csv
import zipfile
import StringIO

for name in glob.glob('C:/Projects/abase/*.zip'):
    base = os.path.basename(name)
    filename = os.path.splitext(base)[0]


    datadirectory = 'C:/Projects/abase/'
    dataFile = filename
    archive = '.'.join([dataFile, 'zip'])
    fullpath = ''.join([datadirectory, archive])
    csv_file = '.'.join([dataFile, 'csv']) #all fixed


    filehandle = open(fullpath, 'rb')
    zfile = zipfile.ZipFile(filehandle)
    data = StringIO.StringIO(zfile.read(csv_file)) #don't forget this line!
    reader = csv.reader(data)

    for row in reader:
        print row