fastcsv - 一次性保存对象表 (ruby)

发布于 2024-08-20 18:51:42 字数 1063 浏览 5 评论 0原文

我使用下面的行读取我的 csv

data = FCSV.table("test.csv", {:quote_char => '"', :col_sep =>',', :row_sep =>:auto, :headers => true, :return_headers => false, :header_converters => :downcase, :converters => :all} )

问题

我可以以相同的方式保存对象数据吗(一行一走 + csv 选项)? 参见上文

我对表格进行排序(请参阅下面的代码),然后我想再次保存它。我不知道如何一次性保存该表。不过我知道如何逐行进行。

array_of_arrays = data.to_a()
headers = array_of_arrays.shift # remove the headers
array_of_arrays.sort_by {|e| [e[3], e[4].to_s, e[1]]} .each {|line| p line }
array_of_arrays.insert(0,headers)

我尝试的任何方法都不起作用,并给了我非常类似于

csv.rb:33: syntax error, unexpected '{', expecting ')'
... FCSV.table("sorted.csv","w" {:quote_char => '"', :col_sep =...

注意:

请注意,我想在保存文件时使用所有 CSV 选项 {:quote_char => '"', :col_sep =>',', :row_sep =>:auto, :headers => true, :return_headers => false, :header_converters => :downcase, :converters => :all }

I read my csv using the line below

data = FCSV.table("test.csv", {:quote_char => '"', :col_sep =>',', :row_sep =>:auto, :headers => true, :return_headers => false, :header_converters => :downcase, :converters => :all} )

QUESTION

Can I save object data in the same manner (one line, one go + csv options)? see above

I sort the table (see the code below) and then I want so save it again. I couldn't work out how to save the table in one go. I know how to do it row by row though.

array_of_arrays = data.to_a()
headers = array_of_arrays.shift # remove the headers
array_of_arrays.sort_by {|e| [e[3], e[4].to_s, e[1]]} .each {|line| p line }
array_of_arrays.insert(0,headers)

Anything I tried did not work and gave me something very similar to

csv.rb:33: syntax error, unexpected '{', expecting ')'
... FCSV.table("sorted.csv","w" {:quote_char => '"', :col_sep =...

NOTE:

Please note that I want to use all the CSV options when saving the file {:quote_char => '"', :col_sep =>',', :row_sep =>:auto, :headers => true, :return_headers => false, :header_converters => :downcase, :converters => :all}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离鸿 2024-08-27 18:51:42

由于 data 中有一个数组数组,因此看起来您可以这样做:

FCSV::Table.new(data).to_csv

以字符串形式获取数据的所有 csv,然后将其输出回文件。

Since you've got an array of arrays in data, it looks like you can just do:

FCSV::Table.new(data).to_csv

to get all the csv for data as a string, then output that back to the file.

吻安 2024-08-27 18:51:42

只是按照 dunedain 所说的进行操作,下面将写出该文件,

@csv = FCSV::Table.new(data).to_csv
File.open("modified_csv.csv", 'w') {|f| f.write(@csv) }

并且您在下面的代码中遇到的错误是因为您在“w”之后和 { 之前没有逗号,但看起来您可能正在尝试读取器函数而不是写入器函数

csv.rb:33: syntax error, unexpected '{', expecting ')'
... FCSV.table("sorted.csv","w" {:quote_char => '"', :col_sep =...

Just following up on what dunedain said, the following will write the file out

@csv = FCSV::Table.new(data).to_csv
File.open("modified_csv.csv", 'w') {|f| f.write(@csv) }

also the error you had in the code below is because you didnt have a comma after the "w" and before the { but it looks like you were perhaps tring to the reader functions instead of the writer functions

csv.rb:33: syntax error, unexpected '{', expecting ')'
... FCSV.table("sorted.csv","w" {:quote_char => '"', :col_sep =...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文