【问题标题】:Splitting a csv file into multiple files将 csv 文件拆分为多个文件
【发布时间】:2019-03-23 20:08:10
【问题描述】:

我有一个 150500 行的 csv 文件,我想将其拆分为包含 500 行(条目)的多个文件

我正在使用 Jupyter,我知道如何打开和读取文件。但是,我不知道如何指定一个 output_path 来记录新创建的文件从拆分大文件。

我在网上找到了这段代码,但由于我不知道我的 output_path 是什么,我不知道如何使用它。此外,对于这段代码,我不明白我们如何指定输入文件。

import os

def split(filehandler, delimiter=',', row_limit=1000,
          output_name_template='output_%s.csv', output_path='.', keep_headers=True):
    import csv
    reader = csv.reader(filehandler, delimiter=delimiter)
    current_piece = 1
    current_out_path = os.path.join(
        output_path,
        output_name_template % current_piece
    )
    current_out_writer = csv.writer(open(current_out_path, 'w'), delimiter=delimiter)
    current_limit = row_limit
    if keep_headers:
        headers = reader.next()
        current_out_writer.writerow(headers)
    for i, row in enumerate(reader):
        if i + 1 > current_limit:
            current_piece += 1
            current_limit = row_limit * current_piece
            current_out_path = os.path.join(
                output_path,
                output_name_template % current_piece
            )
            current_out_writer = csv.writer(open(current_out_path, 'w'), delimiter=delimiter)
            if keep_headers:
                current_out_writer.writerow(headers)
        current_out_writer.writerow(row)

我的文件名为 DataSet2.csv,它与我的 ipynb 笔记本运行时在 jupyter 中的同一个文件中。

【问题讨论】:

    标签: csv split


    【解决方案1】:
    number_of_small_files = 301
    lines_per_small_file = 500
    
    largeFile = open('large.csv', 'r')
    header = largeFile.readline()
    
    for i in range(number_of_small_files):
        smallFile = open(str(i) + '_small.csv', 'w')
    
        smallFile.write(header) # This line copies the header to all small files
    
        for x in range(lines_per_small_file):
            line = largeFile.readline()
            smallFile.write(line)
    
        smallFile.close()
    
    largeFile.close()
    

    这将在同一目录中创建许多小文件。其中大约 301 个。它们的名称将从0_small.csv300_small.csv

    【讨论】:

    • 您发布的代码似乎与拆分行无关。 Filehandler 是 open() 的返回值。您将其作为第一个参数发送到您的代码。但同样,这不是按行分割的。我没有测试它,但是这段代码应该把文件分成多个文件。我现在要睡觉了,但我明天再检查一下,看看您是否有任何问题。
    • with open('DataSet2.csv', "r") as inputFile: for i in range(int(150500/500)): with open(str(i) + "_small.csv" , "w") 作为输出文件:lines = inputFile.readlines(500) outputFile.write(str(lines))
    • 这段代码确实创建了 300 个小文件,但是,它将它们保存在一行中。我怎样才能改变它,以便它在前一行下添加每一行?另外,如何跳过我的 large.csv 文件中的第一行,因为该行属于标题,并且它抵消了我希望在每个小文件中拥有的 500 个数据点?非常感谢您的帮助,非常感谢!
    • 我也很抱歉,该文件有 150500 不包括标题。因此,对于标题,我有 150501 行,我想在其中跳过读取第一(标题)行。
    • 我的回答实际上是错误的。 readline(n) 读取 n 个字符,而不是 n 行。我编辑了我的答案。这应该正确地将所有行写在单独的行中。这也将标头复制到所有小文件。要停止这种情况,请删除 smallFile.write(header) 行。
    【解决方案2】:

    使用标准的 unix 实用程序:

    cat DataSet2.csv | tail -n +2 | split -l 500 --additional-suffix=.csv output_
    

    此管道获取原始文件,用 'tail -n +2' 删除第一行,然后将其余部分分成 500 行块,这些块放入名称以 'output_' 开头并以 ' 结尾的文件中.csv'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多