fastercsv

Importing CSV data into a ruby array/variable

北慕城南 提交于 2019-12-05 02:52:55
问题 I am trying to use a CSV as a settings file in a plugin for the SiriProxy project to use wake-on-lan. This project is based on ruby. So the csv is as follows: Name, MACAddress Desktop, 01-23-45-67-89-ab Computer, 02-46-81-02-46-cd and so on... So what I would like to happen is that when the variable userAction is "Desktop" for instance, then I query the CSV and it returns the MAC address into another variable. I am lost on how to do this. I have seen the csv and faster_csv but do not know how

Overcoming a basic problem with CSV parsing using the FasterCSV gem

纵饮孤独 提交于 2019-12-03 23:32:36
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\",\"two\",\"three\"") => ["one", "two", "three"] # Quotes around fields but with a space after the first

Importing CSV data into a ruby array/variable

梦想与她 提交于 2019-12-03 21:02:33
I am trying to use a CSV as a settings file in a plugin for the SiriProxy project to use wake-on-lan. This project is based on ruby. So the csv is as follows: Name, MACAddress Desktop, 01-23-45-67-89-ab Computer, 02-46-81-02-46-cd and so on... So what I would like to happen is that when the variable userAction is "Desktop" for instance, then I query the CSV and it returns the MAC address into another variable. I am lost on how to do this. I have seen the csv and faster_csv but do not know how to get those to work like this. Thanks in advance! If you try to use FasterCSV in Ruby 1.9 you get a

Speed up csv import

假如想象 提交于 2019-12-03 15:52:34
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] # logger.info("1) ") # logger.info(ename) # logger.info("---") # logger.info(esupp) #---- #ename = row[4]

Pulling a value from one CSV based on a value in another

你。 提交于 2019-12-02 21:20:55
问题 I am trying to figure out the best way to pull a value from a CSV file called lookup.csv based on a value in master.csv , and then save the new file as output.csv . In the example below, the master file has three columns of data with the last column being City . I'd like to replace the City name with the City Code from the lookup.csv file. I don't have a DB that I can lookup from so I am having to use the CSV file. I am trying to use FasterCSV with Ruby 1.8.7. Example File Structure: master

FasterCSV: several separators

白昼怎懂夜的黑 提交于 2019-12-01 20:44:26
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 content (not a delimiter) is zero. So usage of the two different delimiters in the same file is not

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

不打扰是莪最后的温柔 提交于 2019-12-01 20:03:36
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 here... next end end } The MalformedCSVError is caused in the csv.shift method. How can I access the

Import CSV in batches of lines in Rails?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 11:35:58
I'm using FasterCSV to import an uploaded file to a model, and it's working great for small files. However when I try to import a large dataset (21,000 lines) it takes ages and I get browser timeouts on the live server. This is my current working code: logcount=0 Attendee.transaction do FCSV.new(file, :headers => true).each do |row| row[1] = Date.strptime(row[1], '%m/%d/%Y') record = @event.attendees.new(:union_id => row[0], :dob => row[1], :gender => row[2]) if record.save logcount += 1 end end end I'd love to use a background process, but the user needs to see how many lines were imported

Import CSV in batches of lines in Rails?

∥☆過路亽.° 提交于 2019-12-01 08:16:18
问题 I'm using FasterCSV to import an uploaded file to a model, and it's working great for small files. However when I try to import a large dataset (21,000 lines) it takes ages and I get browser timeouts on the live server. This is my current working code: logcount=0 Attendee.transaction do FCSV.new(file, :headers => true).each do |row| row[1] = Date.strptime(row[1], '%m/%d/%Y') record = @event.attendees.new(:union_id => row[0], :dob => row[1], :gender => row[2]) if record.save logcount += 1 end

Replacing text in one CSV column using FasterCSV

眉间皱痕 提交于 2019-11-30 10:00:49
问题 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