如何使用FasterCSV更改CSV文件中的标题然后保存新标题?(How do you change headers in a CSV File with FasterCSV then save th

编程入门 行业动态 更新时间:2024-10-26 12:25:33
如何使用FasterCSV更改CSV文件中的标题然后保存新标题?(How do you change headers in a CSV File with FasterCSV then save the new headers?)

我无法理解FasterCSV中的:header_converters和:转换器。 基本上,我想要做的就是将列标题更改为适当的列名称。

就像是:

FasterCSV.foreach(csv_file, {:headers => true, :return_headers => false, :header_converters => :symbol, :converters => :all} ) do |row| puts row[:some_column_header] # Would be "Some Column Header" in the csv file.

执行我不明白:符号和:全部在转换器参数中。

I'm having trouble understanding the :header_converters and :converters in FasterCSV. Basically, all I want to do is change column headers to their appropriate column names.

something like:

FasterCSV.foreach(csv_file, {:headers => true, :return_headers => false, :header_converters => :symbol, :converters => :all} ) do |row| puts row[:some_column_header] # Would be "Some Column Header" in the csv file.

execpt I don't umderstand :symbol and :all in the converter parameters.

最满意答案

:all转换器意味着它会尝试所有内置转换器,具体为:

:integer: Converts any field Integer() accepts. :float: Converts any field Float() accepts. :date: Converts any field Date::parse() accepts. :date_time: Converts any field DateTime::parse() accepts.

本质上,这意味着它会尝试将任何字段转换为这些值(如果可能),而不是将它们保留为字符串。 所以如果你做了row[i] ,它会返回字符串值'9',它将返回一个整数值9。

标题转换器改变标题用于索引行的方式。 例如,如果做这样的事情:

FastCSV.foreach(some_file, :header_converters => :downcase) do |row|

您可以将标题为“Some Header”的列索引为row['some header'] 。

如果你使用:symbol ,你可以用row[:some_header]索引它。 Symbol降低标题名称,用下划线替换空格,并删除除az,0-9和_之外的其他字符。 这很有用,因为符号的比较比字符串的比较要快得多。

如果你想用row['Some Header']索引一列,那么就不要提供:header_converter选项。


编辑:

作为对你的评论的回应,我害怕headers_convert不会做你想做的。 它不会更改标题行的值,只是将它们用作索引。 相反,您必须使用:return_headers选项,检测标题行并进行更改。 要更改文件并将其重新写出,可以使用如下所示的内容:

require 'fastercsv' input = File.open 'original.csv', 'r' output = File.open 'modified.csv', 'w' FasterCSV.filter input, output, :headers => true, :write_headers => true, :return_headers => true do |row| change_headers(row) if row.header_row? end input.close output.close

如果您需要完全替换原始文件,请在完成上述操作后添加以下行:

FileUtils.mv 'modified.csv', 'original.csv', :force => true

The :all converter means that it tries all of the built-in converters, specifically:

:integer: Converts any field Integer() accepts. :float: Converts any field Float() accepts. :date: Converts any field Date::parse() accepts. :date_time: Converts any field DateTime::parse() accepts.

Essentially, it means that it will attempt to convert any field into those values (if possible) instead of leaving them as a string. So if you do row[i] and it would have returned the String value '9', it will instead return an Integer value 9.

Header converters change the way the headers are used to index a row. For example, if doing something like this:

FastCSV.foreach(some_file, :header_converters => :downcase) do |row|

You would index a column with the header "Some Header" as row['some header'].

If you used :symbol instead, you would index it with row[:some_header]. Symbol downcases the header name, replaces spaces with underscores, and removes characters other than a-z, 0-9, and _. It's useful because comparison of symbols is far faster than comparison of strings.

If you want to index a column with row['Some Header'], then just don't provide any :header_converter option.


EDIT:

In response to your comment, headers_convert won't do what you want, I'm afraid. It doesn't change the values of the header row, just how they are used as an index. Instead, you'll have to use the :return_headers option, detect the header row, and make your changes. To change the file and write it out again, you can use something like this:

require 'fastercsv' input = File.open 'original.csv', 'r' output = File.open 'modified.csv', 'w' FasterCSV.filter input, output, :headers => true, :write_headers => true, :return_headers => true do |row| change_headers(row) if row.header_row? end input.close output.close

If you need to completely replace the original file, add this line after doing the above:

FileUtils.mv 'modified.csv', 'original.csv', :force => true

更多推荐

本文发布于:2023-07-26 17:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1278920.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标题   如何使用   文件   CSV   FasterCSV

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!