使用以下代码拆分大型csv文件。但是,拆分某些文件时,它会在每行之间添加空格。在下面的代码中,我可以告诉代码不要在输出的行之间创建空格吗?我相信我尝试拆分的某些文件可能会嵌入LF或CR,如果这有助于提供更多信息,则会导致它。代码如下:
import os
def split(filehandler, delimiter=',', row_limit=25000,
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)