如何使用FasterCSV更改CSV文件中的标题,然后保存新标题? [英] How do you change headers in a CSV File with FasterCSV then save the new headers?

查看:120
本文介绍了如何使用FasterCSV更改CSV文件中的标题,然后保存新标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解FasterCSV中的:header_converters和:converters.基本上,我要做的就是将列标题更改为其相应的列名称.

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.

类似:

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我不理解转换器参数中的:symbol和:all.

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

推荐答案

:all转换器意味着它将尝试所有内置转换器,特别是:

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.

从本质上讲,这意味着它将尝试将任何字段转换为那些值(如果可能),而不是将其保留为字符串.因此,如果您执行row[i]并且它将返回字符串值'9',则它将返回整数值9.

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|

您将为标题"Some Header"为row['some header']的列建立索引.

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

如果改为使用:symbol,则应使用row[:some_header]对其进行索引. Symbol将标题名称小写,用下划线替换空格,并删除a-z,0-9和_以外的字符.之所以有用,是因为符号比较比字符串比较要快得多.

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.

如果您想使用row['Some Header']为一列编制索引,则只需不提供任何:header_converter选项.

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

恐怕,根据您的评论,headers_convert不会做您想要的.它不会更改标题行的值,而只会更改它们如何用作索引.相反,您必须使用:return_headers选项,检测标题行,然后进行更改.要更改文件并再次将其写出,可以使用以下命令:

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

这篇关于如何使用FasterCSV更改CSV文件中的标题,然后保存新标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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