使用Ruby CSV :: Writer生成包含换行符的字段 [英] Generating fields containing newline with Ruby CSV::Writer
本文介绍了使用Ruby CSV :: Writer生成包含换行符的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想让CSV :: Writer在带引号的字符串内生成换行符:
I want to make CSV::Writer generate a line break within a quoted string:
A,B,"Line
Line",C
以便该行在Excel中显示为:
So that the row would display in Excel as:
A,B,Line,C
Line
是否可以防止CSV:Writer删除换行符? 如果没有,切换到FasterCSV是否可以解决此问题?
Is it possible to prevent CSV:Writer from stripping out newlines? If not, would switching to FasterCSV solve this problem?
推荐答案
当前CSV模块在单个单元格中支持换行符.
The current CSV module supports newline characters in individual cells.
在IRB会话中:
require 'csv'
CSV.open("./testfile.csv", "w") do |csv|
csv << ["row", "of", "CSV\nCSV", "data"]
end
切换到FasterCSV也将起作用.在另一个IRB会话中:
Switching to FasterCSV will also work. From another IRB session:
require 'fastercsv'
FasterCSV.open("./testfile.csv", "w") do |csv|
csv << ["row", "of", "CSV\nCSV", "data"]
end
这篇关于使用Ruby CSV :: Writer生成包含换行符的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文