gpt4 book ai didi

ruby-on-rails - 如何将数据附加到csv文件

转载 作者:数据小太阳 更新时间:2023-10-29 08:14:28 26 4
gpt4 key购买 nike

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

这是我正在做的。

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'' Write-only, starts at end of file if file exists, otherwisecreates a new file for writing.

``a+'' Read-write, starts at end offile if file exists, otherwise creates a new file for reading andwriting

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

关于ruby-on-rails - 如何将数据附加到csv文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11426732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com