将CSV.open
存储的数据在内存中,并写入文件一次当块退出,或者它会自动在多批写?
require 'csv' CSV.open('result.csv', 'wb') do |csv| while row = next_row csv << row end end
user513951.. 8
CSV.open
当块关闭时,它将写入底层操作系统,并且每次缓冲区填满并刷新时它也会写入,这将自动发生.(在我的Ruby安装中,它发生在8196字节.)您还可以添加csv.flush
到块中以强制它按顺序写入.
require 'csv' CSV.open('result.csv', 'wb') do |csv| while row = next_row csv << row # Writes to underlying OS only when buffer fills csv.flush # Forces write to underlying OS end end # Writes to underlying OS and closes file handle
(请注意,写入操作是基础操作系统,它可能会在实际写入磁盘之前再次缓冲流.)
CSV.open
当块关闭时,它将写入底层操作系统,并且每次缓冲区填满并刷新时它也会写入,这将自动发生.(在我的Ruby安装中,它发生在8196字节.)您还可以添加csv.flush
到块中以强制它按顺序写入.
require 'csv' CSV.open('result.csv', 'wb') do |csv| while row = next_row csv << row # Writes to underlying OS only when buffer fills csv.flush # Forces write to underlying OS end end # Writes to underlying OS and closes file handle
(请注意,写入操作是基础操作系统,它可能会在实际写入磁盘之前再次缓冲流.)