我目前正在嘗試將我的 csv 檔案拆分為多個檔案,每個拆分的開頭相互重疊(例如:檔案 1 將是第 1-4000 行,然后第 2 行將是 3000-7000,第 3 行將是6000-10000等)
chunk_size = 4000
def write_chunk(part, lines):
with open('data_part_' str(part) '.csv', 'w') as f_out:
f_out.write(header)
f_out.writelines(lines)
with open("8-0new2.csv", "r") as f:
count = 0
header = f.readline()
lines = []
# for line in f:
for line in range():
count = 1
lines.append(line)
if count % chunk_size == 0:
write_chunk(count // chunk_size, lines)
lines = []
# write remainder
if len(lines) > 0:
write_chunk((count // chunk_size) 1, lines)
這是我當前將 csv 拆分為 4 個檔案的代碼,有什么想法可以改進它,以便它可以撰寫具有重疊行的 csv 嗎?
uj5u.com熱心網友回復:
我沒有資料可以對此進行徹底測驗,但應該可以:
CHUNK = 4_000
OVERLAP = 1_000
def write_csv(lines, filename, header):
with open(filename, 'w') as csv:
csv.write(header)
csv.writelines(lines)
def get_csv_gen():
part = 1
while True:
yield f'data_part_{part}.csv'
part = 1
get_csv_name = get_csv_gen()
with open('8-0new2.csv') as csv:
header = csv.readline()
lines = csv.readlines()
for offset in range(0, len(lines), CHUNK-OVERLAP):
write_csv(lines[offset:offset CHUNK], next(get_csv_name), header)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/490624.html
