根据行值将csv拆分为较小的文件

时间:2018-05-30 16:36:43

标签: python csv

我有一个大型csv,其中包含以下标题列idtypestatelocation

以及以下值:

124, preschool, Pennsylvania, Pittsburgh
421, secondary school, Ohio, Cleveland
213, primary school, California, Los Angeles
155, secondary school, Pennsylvania, Pittsburgh
etc...

该文件未订购,我想为每种类型的学校提供​​csv文件。

我找到的答案是关于有序的csv文件,或者在特定行数后拆分它们。

编辑: 我发现了我想要的东西:

import csv

csv_file = 'school.csv'
value = 'preschool'

with open(csv_file, 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
    for row in spamreader:
        if value in row:
            with open(value + '.csv', 'ab') as myfile:
                spamwriter = csv.writer(myfile)
                spamwriter.writerow(row)
                myfile.close()

并保留标题列我只是将它们从原始列中复制粘贴

1 个答案:

答案 0 :(得分:0)

您只需要进行行比较。然后根据匹配将其附加到不同的文件。

此外,try / except检查文件是否存在,如果不是则写入标头。这是实现您正在寻找的结果的最简单方法。如果文件确实存在以确保标题以您选择的方式进行格式化,您也可以进行比较,但这个答案已经超出了原始问题的范围。

import csv


csv_file = 'file/path/file_name'

values = ['preschool', 'secondary school']


def csv_header(x):
    with open(x + '.csv', 'ab') as myfile:
        myfile.write("%s %s %s %s \n" % ('id', 'type', 'state', 'location'))
        myfile.close()


def csv_writer(y, value):
    for row in y:
        if value in row:
            with open(value + '.csv', 'ab') as myfile:
                spamwriter = csv.writer(myfile)
                spamwriter.writerow(row)
                myfile.close()


def csv_reader(z):
    with open(z + '.csv', 'rb') as spam:
        spamreader = csv.reader(spam, delimiter=',', quotechar='|')
        csv_writer(spamreader, value)


for value in values:
    try:
        csv_reader(value)
        csv_reader(csv_file)

    except:
        csv_header(value)
        csv_reader(csv_file)

https://docs.python.org/2/library/csv.html