如何将数据附加到csv文件 [英] How to append data to csv file

查看:75
本文介绍了如何将数据附加到csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据附加到 csv 文件.我使用 html 表单从用户那里获取该数据.我当前的代码将数据添加到 csv 文件,但没有追加.因此,当用户输入他们的详细信息时,点击注册按钮,然后它会删除之前添加的数据,并在该位置插入新数据.那么如何添加新数据,以便将其保存在下一行并将其余数据保留在原位?

I want to append data to csv file. I get that data from user using html form. My current code adds data to csv file but it does not append. So when user enters their detail hit register button then it erase the previously added data the insert the new one in that place. So how can I append new data so that it saves in the next row and keeps the rest of the data in place?

这就是我正在做的事情.

Here is what I am doing.

full_name = params["full_name"]
email = params["email"]
phone = params["phone"]
organization = params["organization"]
job = params["job"]
diet = params["diet"]
address = params["address"]
code = params["code"]
city = params["city"]
cost_pool = params["cost_pool"]

CSV.open("participants_info.csv", "wb") do |csv|
  csv << [full_name, email, phone, organization, job, diet, address, code, city, cost_pool]
end

推荐答案

``a'' 只写,如果文件存在,则从文件末尾开始,否则创建一个用于写入的新文件.

``a'' Write-only, starts at end of file if file exists, otherwise creates a new file for writing.

``a+'' 读写,从末尾开始file 如果文件存在,否则创建一个新文件用于读取和写作

``a+'' Read-write, starts at end of file if file exists, otherwise creates a new file for reading and writing

使用 CSV.open("participants_info.csv", "a+")

这篇关于如何将数据附加到csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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