【问题标题】:Split Excel file without Opening无需打开即可拆分 Excel 文件
【发布时间】:2019-11-27 16:07:43
【问题描述】:

我在 Hackathons 或 Kaggle 比赛期间获得了大小为 Gb 的 excel 文件。如果我直接打开它或在 python 或 R 中加载它,我的 8gb i7 intel 系统会崩溃。 我想知道是否有任何方法可以在不打开文件的情况下拆分文件

【问题讨论】:

标签: python excel


【解决方案1】:
 Splits a CSV file into multiple pieces.

A quick bastardization of the Python CSV library.

Arguments:

    `row_limit`: The number of rows you want in each output file. 10,000 by default.
    `output_name_template`: A %s-style template for the numbered output files.
    `output_path`: Where to stick the output files.
    `keep_headers`: Whether or not to print the headers in each output file.

Example usage:

    >> from toolbox import csv_splitter;
    >> csv_splitter.split(open('/home/ben/input.csv', 'r'));
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 = next(reader)
    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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    相关资源
    最近更新 更多