拆分.csv同时保持描述第一行

时间:2016-10-21 01:51:45

标签: python python-2.7 csv

我有这段代码:

#!/usr/bin/env python

# Import module
import os

# Define file_splitter function
def file_splitter(fullfilepath, lines=50):
  """Splits a plain text file based on line count."""
    path, filename = os.path.split(fullfilepath)
    basename, ext = os.path.splitext(filename)

# Open source text file
    with open(fullfilepath, 'r') as f_in:
        try:
        # Open first output file
            f_output = os.path.join(path, '{}_{}{}'.format(basename, 0, ext))
            f_out = open(f_output, 'w')

        # Read input file one line at a time
        for i, line in enumerate(f_in):
        # When current line can be divided by the line
        # count close the output file and open the next one
            if i % lines == 0:
                f_out.close()
                f_output = os.path.join(path, '{}_{}{}'.format(basename, i, ext))
                f_out = open(f_output, 'w')

            # Write current line to output file
            f_out.write(line)

        finally:
        # Close last output file
            f_out.close()

# Call function with source text file and line count
file_splitter('Products_con_almacen_stock.csv', 12000)

这会将文件Products_con_almacen_stock.csv拆分为120.000行的块。

现在,每个块都有列和行,但没有标题,只有第一个块有它,我想保留每个块上的第一个描述行。

这可能吗?

提前致谢!

1 个答案:

答案 0 :(得分:2)

你可以这样做:

first = True
for line in lines:
    if first: 
        header = line
        first = False
    ....

然后,您可以在所有后续文件中使用标题。