简体   繁体   English

Ruby smarter_csv标头问题

[英]Ruby smarter_csv header issues

I am trying to create a Ruby file that extracts data. 我正在尝试创建一个提取数据的Ruby文件。 I seem to run into an issue as the provided CSV files don't have headers attached. 我似乎遇到了问题,因为提供的CSV文件没有附加标题。 So, I am trying to tell smarter_csv that there are NO headers. 因此,我试图告诉smarter_csv没有头。 The code, I use for this is: 我为此使用的代码是:

transactions = SmarterCSV.process(file, {chunk_size: 20,remove_empty_values: false, remove_zero_values: false, headers_in_file: false, user_provided_headers:{timestamp: :timestamp, location: :loc, lat: :lat, long: :long, rfid: :rfid, start: :tart, ending: :end, type: :type , amount: :amount, status: :status}})

I also, add some new headers so they map right. 我也添加一些新的标题,以便它们正确映射。 The result unfortunately is an error, which is 不幸的是,结果是一个错误,即

/gems/smarter_csv-1.0.19/lib/smarter_csv/smarter_csv.rb:64:in process': undefined method map!' /gems/smarter_csv-1.0.19/lib/smarter_csv/smarter_csv.rb:64:进行process': undefined method图! for nil:NilClass (NoMethodError) 对于nil:NilClass(NoMethodError)

I thought there might be something wrong with the passing of "false", so I changed it to a String, but that resulted unfortunately in that there were no custom headers added. 我认为传递“ false”可能有问题,因此我将其更改为String,但是不幸的是,结果没有添加自定义标头。 I was wondering if anyone else is struggling with the headers in CSV. 我想知道是否还有其他人正在为CSV中的标头苦苦挣扎。 Maybe there is an easy solution, or maybe we need to contribute to the GEM. 也许有一个简单的解决方案,或者我们需要为GEM做出贡献。 Thanks! 谢谢!

I also, add some new headers so they map right. 我也添加一些新的标题,以便它们正确映射。

 user_provided_headers:{timestamp: :timestamp, location: :loc, lat: :lat, long: :long, rfid: :rfid, start: :tart, ending: :end, type: :type , amount: :amount, status: :status} 

:user_provided_headers is supposed to be an Array not a Hash: :user_provided_headers 应该是一个数组而不是哈希:

user provided Array of header strings or symbols, to define 用户提供的标题字符串或符号数组,用于定义
what headers should be used, overriding any in-file headers. 应该使用什么标题,覆盖所有文件内标题。
You can not combine the :user_provided_headers and :key_mapping options. 您不能结合使用:user_provided_headers和:key_mapping选项。

... ...

the provided CSV files don't have headers attached 提供的CSV文件没有标头

If the csv file has no headers, the following makes no sense: 如果csv文件没有标题,则以下内容没有意义:

location: :loc

What would :location refer to? :location指的是什么? That seems to be saying that the header called :location in the file should be called :loc in the output. 这似乎是说文件中名为:location的标头应在输出中称为:loc smarter_csv has a setting for mapping existing headers in a file to new names in the output: smarter_csv有一个设置,用于将文件中的现有标头映射到输出中的新名称:

:key_mapping -- hash which maps headers from the CSV file to keys in the result hash :key_mapping-哈希将CSV文件中的标头映射到结果哈希中的键

But you claim your csv file has no headers, and you are also telling smarter_csv that your file has no headers: headers_in_file: false . 但是您声称您的csv文件没有标题,并且您还告诉smarter_csv文件没有标题: headers_in_file: false

Here are some examples of how things work: 以下是一些有关工作原理的示例:

csv.csv: csv.csv:

0,1,2
3,,5

And some code: 和一些代码:

require 'smarter_csv'

data = SmarterCSV.process(
  'csv.csv',
  {
    headers_in_file: false,
    user_provided_headers: %i[x y z],
    remove_empty_values: false,
    remove_zero_values: false,
  }
)

p data

--output:--
[{:x=>0, :y=>1, :z=>2}, {:x=>3, :y=>"", :z=>5}]

If you tell smarter_csv that your file doesn't contain headers with headers_in_file: false , and then you provide a :key_mapping , which tells smarter csv to map the headers in the file to new names in the output, then you will get an error: 如果您告诉smarter_csv文件不包含带有headers_in_file: false标题,然后提供:key_mapping ,它告诉更聪明的csv将文件中的标题映射到输出中的新名称,那么您将得到一个错误:

require 'smarter_csv'

data = SmarterCSV.process(
  'csv.csv',
  {
    headers_in_file: false,
    key_mapping: {'x' => 'x_val', 'y' => 'y_val', 'z' => 'z_val'},
    remove_empty_values: false,
    remove_zero_values: false,
  }
)

p data

--output:--
/Users/7stud/.rvm/gems/ruby-2.1.2/gems/smarter_csv-1.0.19/lib/smarter_csv/smarter_csv.rb:64:in `process': undefined method `map!' for nil:NilClass (NoMethodError)
    from 1.rb:3:in `<main>'

Now, if your csv file actually contains headers, headers_in_file: true , then providing a :key_mapping option makes sense: 现在,如果您的csv文件实际上包含标头headers_in_file: true ,则提供:key_mapping选项很有意义:

csv.csv: csv.csv:

x,y,z
0,1,2
3,,5

The code: 编码:

require 'smarter_csv'

data = SmarterCSV.process(
  'csv.csv',
  {
    headers_in_file: true,
    key_mapping: {x: 'x_val', y: 'y_val', z: 'z_val'},  #keys must be symbols
    remove_empty_values: false,
    remove_zero_values: false,
  }
)

p data

--output:--
[{:x_val=>0, :y_val=>1, :z_val=>2}, {:x_val=>3, :y_val=>"", :z_val=>5}]

Based on your current understanding of smarter_csv, I suggest you reconsider whether you should be using the :chunk_size option. 根据您目前对smarter_csv的了解,建议您重新考虑是否应该使用:chunk_size选项。 If you don't know, don't. 如果您不知道,那就不要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM