如何正确写入CSV文件 [英] How write into CSV file properly

查看:138
本文介绍了如何正确写入CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ruby 1.9.2并使用其csv库。我想在csv中正确地写



像这样

  name,country_code,destination,code 
阿富汗,93,Bamain,51
阿富汗,93,Bamain,52
阿富汗,93,Bamain,53
阿富汗,93,Parwan,91

  def export_data 
@coun = Country.all(:limit => 10)
header = name,country_code,destination,code
file =my_file.csv
File.open(file,w)do | csv |
csv<< header
@ coun.each do | c |
csv<< [c.name,c.country_code,c.user_id,c.subscriber_id]
#如何在这里换行
end
end
send_file(file)
end

我上面提到了如何在CSV文件中将换行符放在那里, p>

涵盖CSV[]中的每一行

  ,1,1,2334] 

提前感谢..

解决方案

  csv< \\\

Stackoverflow在答案中需要30个字符,但我不知道还有什么


i am using ruby 1.9.2 and also use its csv library.I want to write in csv properly just

like this

name,country_code,destination,code
Afghanistan,93,Bamain,51
Afghanistan,93,Bamain,52
Afghanistan,93,Bamain,53
Afghanistan,93,Parwan,91

My code is this

def export_data
  @coun = Country.all(:limit => 10)
  header = "name,country_code,destination,code"
  file = "my_file.csv"
  File.open(file, "w") do |csv|
    csv << header
    @coun.each do |c|
      csv << [c.name, c.country_code, c.user_id, c.subscriber_id]       
      # How puts line break here
    end
  end
  send_file(file)
end

I have mention above how puts i line break there in CSV file and also omit this sigh which

covers every row in CSV"[]"

 Like   ["Finland",1,1,2334]

Thanks in advance..

解决方案

csv << "\n"

Stackoverflow requires 30 characters in an answer, but I don't know what more to say.

这篇关于如何正确写入CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆