我想读取 csv 行,更新一个字段,然后再次用引号输出该行。
Row Example Input => "Joe", "Blow", "[email protected] /cdn-cgi/l/email-protection"
Desired Row Example Output => "Joe", "Blow", "[email protected] /cdn-cgi/l/email-protection"
My script below outputs => Joe, Blow, [email protected] /cdn-cgi/l/email-protection
它失去了我想保留的双引号。
我尝试过各种选择,但到目前为止没有任何乐趣..有什么建议吗?
非常感谢!
require 'csv'
CSV.foreach('transactions.csv',
:quote_char=>'"',
:col_sep =>",",
:headers => true,
:header_converters => :symbol ) do |row|
row[:customer_email] = '[email protected] /cdn-cgi/l/email-protection'
puts row
end
CSV 字段中的引号通常是不必要的,除非字段本身包含分隔符或换行符。但您可以强制 CSV 文件始终使用引号。为此,您需要设置force_quotes => true http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV.html#method-c-new:
CSV.foreach('transactions.csv',
:quote_char=>'"',
:col_sep =>",",
:headers => true,
:force_quotes => true,
:header_converters => :symbol ) do |row|
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)