ruby / csv

CSV Reading and Writing
https://ruby.github.io/csv/
BSD 2-Clause "Simplified" License
181 stars 114 forks source link

How to combine mode and options in CSV.foreach? #113

Closed davideluque closed 4 years ago

davideluque commented 4 years ago

I am trying to iterate over a CSV that has a byte order mark (BOM) and headers.

CSV.foreach('file.csv', "r:bom|utf-8") {}

Does the trick to address the byte order mark.

CSV.foreach('file.csv', headers:true){}

Works to take the headers into consideration.

I can't find a way to combine both options.


Ruby version: 2.4.2

kou commented 4 years ago

CSV.foreach("file.csv", "r:bom|utf-8", headers: true) {} will work.

davideluque commented 4 years ago

CSV.foreach("file.csv", "r:bom|utf-8", headers: true) {} will work.

@kou Thanks for your response. I tried your suggestion and I get an error

ArgumentError: wrong number of arguments (given 3, expected 1..2) from /home/david/.rbenv/versions/2.4.2/lib/ruby/2.4.0/csv.rb:1145:in `foreach'

Could you re-open my issue, please?

kou commented 4 years ago

gem install csv will work.

davideluque commented 4 years ago

gem install csv will work.

Thanks, it does. Also gem 'csv' in gemfile.