fastercsv

fastercsv error with ruby 1.9.2

旧街凉风 提交于 2019-11-30 00:30:27
问题 I have an existing rails application I'm running on ruby 1.9.2 and linux its rails version is rails 2.3.8 and it has a GEMFILE as well, in its vendor/gems directory it has 'fastercsv-1.5.4' gem and in its migrations (in two migrations) it has required the gem 'fastercsv' require 'fastercsv' But when I do rake db:migrate it fails the migration claiming "Please switch to Ruby 1.9's standard CSV library. It's FasterCSV plus support for Ruby 1.9's m17n encoding engine." and I have found out the

Replacing text in one CSV column using FasterCSV

人走茶凉 提交于 2019-11-29 18:00:05
Being relatively new to Ruby, I am trying to figure out how to do the following using FasterCSV: Open a CSV file, pick a column by its header, in this column only replace all occurrences of string x with y, write out the new file to STDOUT. The following code almost works: filename = ARGV[0] csv = FCSV.read(filename, :headers => true, :header_converters => :symbol, :return_headers => true, :encoding => 'u') mycol = csv[:mycol] # construct a mycol_new by iterating over mycol and doing some string replacement puts csv[:mycol][0] # produces "MyCol" as expected puts mycol_new[0] # produces "MyCol"

Best way to read CSV in Ruby. FasterCSV?

大城市里の小女人 提交于 2019-11-29 16:03:27
I have a CSV file that I want to read with Ruby and create Ruby objects to insert into a MySQL database with Active Record. What's the best way to do this? I see two clear options: FasterCSV & the Ruby core CSV . Which is better? Is there a better option that I'm missing? EDIT: Gareth says to use FasterCSV, so what's the best way to read a CSV file using FasterCSV? Looking at the documentation, I see methods called parse , foreach , read , open ... It says that foreach "is intended as the primary interface for reading CSV files." So, I guess I should use that one? Ruby 1.9 adopted FasterCSV as

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

旧巷老猫 提交于 2019-11-29 07:39:35
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 => row[5], :sow_inside_outside => row[8] n=n+1 end I've searched for a solution to this problem and have

CSV - Unquoted fields do not allow \r or \n (line 2)

孤人 提交于 2019-11-29 02:51:08
问题 Trying to parse a CSV file, but still getting the error message Unquoted fields do not allow \r or \n (line 2). . I found here at SO similar topic, where was a hint to do following: CSV.open('file.csv', :row_sep => "\r\n") do |csv| but his unfortunately doesn't works me... I can't change the CSV file, so I would need to fix it in the code. EDIT sample of CSV file: A;B;C 1234;... Is there any way to do it? Many thanks! 回答1: First of all, you should set you column delimiters to ';', since that

Heroku file upload problem

允我心安 提交于 2019-11-29 00:42:43
I've been having a problem uploading a CSV file to Heroku and processing it. It works fine in my local environment. Just do be clear, I don't need to save the file on Heroku, just access it during the request in order to convert it into a string for processing and importing into the DB. What I want to do is: Upload CSV file Strip out the header block, depending on which network the report is from Read the CSV data into the DB. This step works fine. Controller code: def create @account = Account.find(params[:report][:account_id]) @file = params[:report][:file].read # logger.info file.inspect

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

半城伤御伤魂 提交于 2019-11-28 20:31:37
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 help would be greatly appreciated. I would recommend to use the CSV-library instead: require 'csv' CSV

How do I robustly parse malformed CSV?

感情迁移 提交于 2019-11-28 06:30:19
I'm processing data from government sources (FEC, state voter databases, etc). It's inconsistently malformed, which breaks my CSV parser in all sorts of delightful ways. It's externally sourced and authoritative. I must parse it, and I cannot have it re-input, validated on input, or the like. It is what it is; I don't control the input. Properties: Fields contain malformed UTF-8 (e.g. Foo \xAB bar ) The first field of a line specifies the record type from a known set. Knowing the record type, you know how many fields there are and their respective data types, but not until you do. Any given

Heroku file upload problem

我们两清 提交于 2019-11-27 15:24:30
问题 I've been having a problem uploading a CSV file to Heroku and processing it. It works fine in my local environment. Just do be clear, I don't need to save the file on Heroku, just access it during the request in order to convert it into a string for processing and importing into the DB. What I want to do is: Upload CSV file Strip out the header block, depending on which network the report is from Read the CSV data into the DB. This step works fine. Controller code: def create @account =

What is Ruby 1.9 standard CSV library?

故事扮演 提交于 2019-11-27 07:43:37
When I try the FasterCSV gem on my application I get this error: Please switch to Ruby 1.9's standard CSV library. It's FasterCSV plus support for Ruby 1.9's m17n encoding engine. By the way, I'm using Rails 3, Ruby 1.9.2, and Rubygems 1.4. Can someone explain to me please how to use the standard CSV library for Ruby 1.9. I don't have any idea at all because I'm very new to Rails. Dylan Markow Ruby 1.9 has adopted FasterCSV as its built-in CSV library. However, it's in the standard library rather than Ruby 1.9's core, so you need to manually require it in your application. After adding a