fastercsv

Generating fields containing newline with Ruby CSV::Writer

六眼飞鱼酱① 提交于 2020-01-14 12:44:34
问题 I want to make CSV::Writer generate a line break within a quoted string: A,B,"Line Line",C So that the row would display in Excel as: A,B,Line,C Line Is it possible to prevent CSV:Writer from stripping out newlines? If not, would switching to FasterCSV solve this problem? 回答1: Switching to FasterCSV will work. From an IRB session: require 'fastercsv' FasterCSV.open("./testfile.csv", "w") do |csv| csv << ["row", "of", "CSV\nCSV", "data"] end 回答2: Looks like you can if you set the row separator

Rails3 CSV putting "" instead of actual quotes

狂风中的少年 提交于 2020-01-03 15:34:14
问题 Similar to this question except I don't use html_safe anywhere in the whole project. I generate a CSV file in index.csv.erb like this: <%= response.content_type = 'application/octet-stream' CSV.generate do |csv| @persons.each do |person| csv << [ person[:name], person[:nickname] ] end end %> PROBLEM: If nickname is NULL in the database (ActiveRecord/MySQL) then the CSV file associated element becomes "" . I would expect "" , or even nothing at all. Result file sample: Nicolas, Nico Joe, ""

FasterCSV: Read Remote CSV Files

℡╲_俬逩灬. 提交于 2020-01-02 07:03:02
问题 I can't seem to get this to work. I want to pull a CSV file from a different webserver to read in my application. This is how I'd like to call it: url = 'http://www.testing.com/test.csv' records = FasterCSV.read(url, :headers => true, :header_converters => :symbol) But that doesn't work. I tried Googling, and all I came up with was this excerpt: Practical Ruby Gems So, I tried modifying it as follows: require 'open-uri' url = 'http://www.testing.com/test.csv' csv_url = open(url) records =

FasterCSV: several separators

空扰寡人 提交于 2019-12-30 22:54:45
问题 My Rails3 app parses user-uploaded CSV files. As can be expected, users upload tab-separated AND comma-separated files. I want to support both. My code: input = CSV.read(uploaded_io.tempfile, { encoding: "UTF-8", :col_sep => "\t"}) QUESTION: How to change it to support commas too? FasterCSV's doc describes col_sep as The String placed between each field. so :col_sep => ",\t" won't work. Note: All data inside are integers or identifiers, so the probability of someone using \t or , within the

FasterCSV: several separators

会有一股神秘感。 提交于 2019-12-30 22:54:43
问题 My Rails3 app parses user-uploaded CSV files. As can be expected, users upload tab-separated AND comma-separated files. I want to support both. My code: input = CSV.read(uploaded_io.tempfile, { encoding: "UTF-8", :col_sep => "\t"}) QUESTION: How to change it to support commas too? FasterCSV's doc describes col_sep as The String placed between each field. so :col_sep => ",\t" won't work. Note: All data inside are integers or identifiers, so the probability of someone using \t or , within the

Export nested entities to CSV file

余生长醉 提交于 2019-12-24 16:28:48
问题 I have a rails app with some nested data that I'd like to export as a CSV file The models look like: class ContainerRecord < ActiveRecord::Base has_many :child_records and class ChildRecord < ActiveRecord::Base belongs_to :container_record I'd like to be able to export a CSV file with each ContainerRecord on a row with its information in the first few columns and a value from each ChildRecord in the remaining columns. I can't guarantee the number of ChildRecords associated with each

Ruby CSV read multiline fields

可紊 提交于 2019-12-24 08:19:54
问题 I exported tables and queries from SQL, where some of the fields are multi-line. The Ruby (1.9+) way to read CSV appears to be: require 'csv' CSV.foreach("exported_mysql_table.csv", {:headers=>true}) do |row| puts row end Which works great if my data is like this: "id","name","email","potato" 1,"Bob","bob@bob.bob","omnomnom" 2,"Charlie","char@char.com","andcheese" 4,"Doug","diggyd@diglet.com","usemeltattack" (The first line is the header/attributes) But if I have: "id","name","address","email

Speed up csv import

≡放荡痞女 提交于 2019-12-21 05:06:25
问题 I want to import big amount of cvs data (not directly to AR, but after some fetches), and my code is very slow. def csv_import require 'csv' file = File.open("/#{Rails.public_path}/uploads/shate.csv") csv = CSV.open(file, "r:ISO-8859-15:UTF-8", {:col_sep => ";", :row_sep => :auto, :headers => :first_row}) csv.each do |row| #ename,esupp= row[1].split(/_/) #(ename,esupp,foo) = row[1]..split('_') abrakadabra = row[0].to_s() (ename,esupp) = abrakadabra.split(/_/) eprice = row[6] eqnt = row[1] #

How do I import using FasterCSV a row with a name like “Ciarán”?

跟風遠走 提交于 2019-12-21 02:55:25
问题 I am trying to load in my data migration a member database. Quite a few of the names have special characters such as "Ciarán". I've set up a simple example like this: require 'rubygems' require 'fastercsv' FasterCSV.foreach("/Users/developer/Work/madmin/db/data/Members.csv") do |row| puts row.inspect end and I get the following: /usr/local/lib/ruby/gems/1.8/gems/fastercsv-1.5.0/lib/faster_csv.rb:1616:in `shift': FasterCSV::MalformedCSVError (FasterCSV::MalformedCSVError) when I hit the row

Best way to read CSV in Ruby. FasterCSV?

不问归期 提交于 2019-12-18 09:21:50
问题 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