eee*_*eee 5 ruby csv large-files
将CSV.open存储的数据在内存中,并写入文件一次当块退出,或者它会自动在多批写?
require 'csv'
CSV.open('result.csv', 'wb') do |csv|
while row = next_row
csv << row
end
end
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
(请注意,写入操作是基础操作系统,它可能会在实际写入磁盘之前再次缓冲流.)
| 归档时间: |
|
| 查看次数: |
2048 次 |
| 最近记录: |