fastercsv

Rails FasterCSV “unquoted fields do not allow \r or \n”

五迷三道 提交于 2019-12-18 05:04:01
问题 I'm having an issue with FasterCSV and my rake db:seeds migration. I get the error: "rake aborted! Unquoted fields do not allow \r or \n (line 2)" on the following seeds.rb data: require 'csv' directory = "db/init_data/" file_name = "gardenzing020812.csv" path_to_file = directory + file_name puts 'Loading Plant records' # Pre-load all Plant records n=0 CSV.foreach(path_to_file) do |row| Plant.create! :name => row[1], :plant_type => row[3], :group => row[2], :image_path => row[45], :height =>

Rails FasterCSV “unquoted fields do not allow \r or \n”

痴心易碎 提交于 2019-12-18 05:03:19
问题 I'm having an issue with FasterCSV and my rake db:seeds migration. I get the error: "rake aborted! Unquoted fields do not allow \r or \n (line 2)" on the following seeds.rb data: require 'csv' directory = "db/init_data/" file_name = "gardenzing020812.csv" path_to_file = directory + file_name puts 'Loading Plant records' # Pre-load all Plant records n=0 CSV.foreach(path_to_file) do |row| Plant.create! :name => row[1], :plant_type => row[3], :group => row[2], :image_path => row[45], :height =>

How to write columns header to a csv file with Ruby?

帅比萌擦擦* 提交于 2019-12-17 22:47:02
问题 I am having trouble writing columns to a csv file with Ruby. Below is my snippet of code. calc = numerator/denominator.to_f data_out = "#{numerator}, #{denominator}, #{calc}" File.open('cdhu3_X.csv','a+') do|hdr| hdr << ["numerator","denominator","calculation\n"] #< column header hdr << "#{data_out}\n" end The code adds the column headers to every line and I only need it at the top of each column of data. I have searched here and other places but can't find a clear answer to how its done. Any

Overcoming a basic problem with CSV parsing using the FasterCSV gem

让人想犯罪 __ 提交于 2019-12-12 08:19:48
问题 I have found a CSV parsing issue with FasterCSV (1.5.0) which seems like a genuine bug, but which I'm hoping there's a workaround for. Basically, adding a space after the separator (in my case a comma) when the fields are enclosed in quotes generates a MalformedCSVError . Here's a simple example: # No quotes on fields -- works fine FasterCSV.parse_line("one,two,three") => ["one", "two", "three"] # Quotes around fields with no spaces after separators -- works fine FasterCSV.parse_line("\"one\"

Ruby scraper. How to export to CSV?

。_饼干妹妹 提交于 2019-12-11 05:25:43
问题 I wrote this ruby script to scrape product info from the manufacturer website. The scraping and storage of the product objects in an array works, but I can't figure out how to export the array data to a csv file. This error is being thrown: scraper.rb:45: undefined method `send_data' for main:Object (NoMethodError) I do not understand this piece of code. What's this doing and why isn't it working right? send_data csv_data, :type => 'text/csv; charset=iso-8859-1; header=present', :disposition

merge CSV files on a common field with ruby/fastercsv

◇◆丶佛笑我妖孽 提交于 2019-12-11 02:38:58
问题 I have a 'master' file with a number of columns: 1 2 3 4 5. I have a few other files, with fewer rows than the master file, each with columns: 1 6. I'd like to merge these files matching on the column 1 field and add column 6 to the master. I've seen some python/UNIX solutions but would prefer to use ruby/fastercsv if it's a good fit. I would appreciate any help getting started. 回答1: FasterCSV is now the default CSV implementation in Ruby 1.9. This code is untested, but should work. require

How can I further process the line of data that causes the Ruby FasterCSV library to throw a MalformedCSVError?

不想你离开。 提交于 2019-12-09 03:37:09
问题 The incoming data file(s) contain malformed CSV data such as non-escaped quotes, as well as (valid) CSV data such as fields containing new lines. If a CSV format error is detected I would like to use an alternative routine on that data. With the following sample code (abbreviated for simplicity) FasterCSV.open( file ){|csv| row = true while row begin row = csv.shift break unless row # Do things with the good rows here... rescue FasterCSV::MalformedCSVError => e # Do things with the bad rows

Ruby on Rails Moving from CSV to FasterCSV

拈花ヽ惹草 提交于 2019-12-08 11:56:19
问题 I currently have the following code to parse a csv file using the standard csv library @parsed_file=CSV::Reader.parse(params[:dump][:file]) @parsed_file.each do |row| #some code end I want to move this to faster csv for the increased speed. Does anyone know the equivalent of the above for FasterCSV? Thanks 回答1: CSV::Reader.parse(File.open('file.csv')){|row| puts row} or CSV::Reader.parse("some, content\nanother, content"){|row| puts row} and FasterCSV.parse(File.open('file.csv')){|row| puts

merge rows csv by id ruby

淺唱寂寞╮ 提交于 2019-12-08 05:16:59
问题 I have a .csv file that, for simplicity, is two fields: ID and comments. The rows of id's are duplicated where each comment field had met max char from whatever table it was generated from and another row was necessary. I now need to merge associative comments together thus creating one row for each unique ID, using Ruby. To illustrate, I'm trying in Ruby, to make this: ID | COMMENT 1 | fragment 1 1 | fragment 2 2 | fragment 1 3 | fragment 1 3 | fragment 2 3 | fragment 3 into this: ID |

How do you change headers in a CSV File with FasterCSV then save the new headers?

牧云@^-^@ 提交于 2019-12-05 18:53:47
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. Pesto The :all converter means that it tries all of the built-in converters, specifically: :integer: Converts any field