Concatenate array and string for CSV - ruby

I'm attempting to concatenate a string to an array value. Since the URL/domain is the same, I simply store the users email prefix and append the url/domain. I need to export the full email address out to CSV:
CSV.generate(options) do |csv|
columns = %w(name, email_address)
url = "#example.com"
all.each do |location|
csv << location.attributes.values_at(*columns) + [url]
end
end
Currently the resulting output is:
Joe, user1, #example.com
Bob, user2, #example.com
What I need is:
Joe, user1#example.com
Bob, user2#example.com
How can I achieve the above?

location is a model object, right?
CSV.generate(options) do |csv|
domain = "example.com"
all.each do |location|
csv << [location.name, "#{location.email_address}##{domain}"]
end
end
Update:
IMO that's cleaner as well. But if you want to keep your version, then I suggest you create a full_email_address method in your Location model which returns something like username#domain.com.
Then, you can vary the columns data later on and easily modify your CSV output. Like so:
class Location << ActiveRecord::Base
def full_email_address
return "" if self.email_address.blank?
domain = "example.com" # or save this as a constant in the class
"#{self.email_address}##{domain}"
end
end
CSV.generate(options) do |csv|
columns = %w{name full_email_address} # add other methods or attributes here
all.each do |location|
csv << columns.map{ |moa| location.public_send(moa) }
end
end

Try this:
CSV.generate(options) do |csv|
columns = %w(name, email_address)
url = "#example.com"
all.each do |location|
row = location.attributes.values_at(*columns)
row[-1] = row[-1] + url
csv << row
end
end
Currently the array written to CSV is ['Joe', 'user1'] + ['#example.com'], so instead of adding url to the attributes array I am adding it to the last attribute.

Related

How do I access the filename of the CSV file I just opened?

I have a method that looks like this:
def extract_websites
websites = []
csvs = Dir["#{#dir_name}/#{#state}/*.csv"]
csvs.each do |csv|
CSV.foreach(csv, headers: true) do |row|
websites << row['Website']
end
end
websites.uniq!
end
But what I need want to do is for each CSV file that is opened, I would like to detect the name of that file.
How do I do that?
In your sample the variable csv holds the path of the CSV file.
That local variable is available in the blocks of its children, it shares its scope down but not upwards.
So:
def extract_websites
websites = []
csvs = Dir["#{#dir_name}/#{#state}/*.csv"]
csvs.each do |csv|
puts File.expand_path(csv) # show the full path for each csv file
CSV.foreach(csv, headers: true) do |row|
puts csv # shows unexpanded path for each row of a csv
websites << row['Website']
end
end
websites.uniq!
end
should print out the path for each CSV file and for each row.

Access csv data from htttp request in ruby

I'm trying to access the csv data, which I recive if I make a http-request.
I don't save it to a csv file, so I save it to the variable.
Let's say this is the response I get, how can I print food?
uuid,event_id,category
12,1,food
13,2,cars
And this is the part of the ruby code which is important.
That's something I found, but it was originally used with a file, so it doesn't work.
csvdata = request(action,parameter)
#data_hash = {}
CSV.foreach(csvdata) do |row|
uuid, event_id, category = row
#data_hash[uuid] = event_id
end
Do I really need files for that or is there a easy way I can access the values?
Update
CSV.parse(csvdata,data = Hash.new) do |row|
puts data
end
The hash should look like this so I can use the column names
{"uuid" => "12,13", "event_id" => "323,3243", "category" => "food,cars"}
csv_data = Hash.new{|k, v| k[v] = []}
CSV.parse(csv_string, headers: true) do |row|
row.each{|k, v| csv_data[k] << v}
end
csv_data = Hash[csv_data.map{|k, v| [k, v.join(",")]}]
Update after specification Requested output.
Try this:
csvdata = request(action,parameter)
#data_hash = {}
CSV.parse(csvdata, headers: true) do |row|
#data_hash[row['uuid']] = row['event_id']
end
#data_hash
# => {"12"=>"1", "13"=>"2"}
When you parse a CSV, the seconds parameter (data = Hash.new in your code) is actually an options parameter. You can see the available options here:
:headers
If set to :first_row or true, the initial row of the CSV file will be treated as a row of headers. If set to an Array, the contents will be used as the headers. If set to a String, the String is run through a call of ::parse_line with the same :col_sep, :row_sep, and :quote_char as this instance to produce an Array of headers. This setting causes #shift to return rows as CSV::Row objects instead of Arrays and #read to return CSV::Table objects instead of an Array of Arrays.
When passing headers: true - values are parsed into a Row object, where they can be accessed by name.

How to map and edit a CSV file with Ruby

Is there a way to edit a CSV file using the map method in Ruby? I know I can open a file using:
CSV.open("file.csv", "a+")
and add content to it, but I have to edit some specific lines.
The foreach method is only useful to read a file (correct me if I'm wrong).
I checked the Ruby CSV documentation but I can't find any useful info.
My CSV file has less than 1500 lines so I don't mind reading all the lines.
Another answer using each.with_index():
rows_array = CSV.read('sample.csv')
desired_indices = [3, 4, 5].sort # these are rows you would like to modify
rows_array.each.with_index(desired_indices[0]) do |row, index|
if desired_indices.include?(index)
# modify over here
rows_array[index][target_column] = 'modification'
end
end
# now update the file
CSV.open('sample3.csv', 'wb') { |csv| rows_array.each{|row| csv << row}}
You can also use each_with_index {} insead of each.with_index {}
Is there a way to edit a CSV file using the map method in Ruby?
Yes:
rows = CSV.open('sample.csv')
rows_array = rows.to_a
or
rows_array = CSV.read('sample.csv')
desired_indices = [3, 4, 5] # these are rows you would like to modify
edited_rows = rows_array.each_with_index.map do |row, index|
if desired_indices.include?(index)
# simply return the row
# or modify over here
row[3] = 'shiva'
# store index in each edited rows to keep track of the rows
[index, row]
end
end.compact
# update the main row_array with updated data
edited_rows.each{|row| rows_array[row[0]] = row[1]}
# now update the file
CSV.open('sample2.csv', 'wb') { |csv| rows_array.each{|row| csv << row}}
This is little messier. Is not it? I suggest you to use each_with_index with out map to do this. See my another answer
Here is a little script I wrote as an example on how read CSV data, do something to data, and then write out the edited text to a new file:
read_write_csv.rb:
#!/usr/bin/env ruby
require 'csv'
src_dir = "/home/user/Desktop/csvfile/FL_insurance_sample.csv"
dst_dir = "/home/user/Desktop/csvfile/FL_insurance_sample_out.csv"
puts " Reading data from : #{src_dir}"
puts " Writing data to : #{dst_dir}"
#create a new file
csv_out = File.open(dst_dir, 'wb')
#read from existing file
CSV.foreach(src_dir , :headers => false) do |row|
#then you can do this
# newrow = row.each_with_index { |rowcontent , row_num| puts "# {rowcontent} #{row_num}" }
# OR array to hash .. just saying .. maybe hash of arrays..
#h = Hash[*row]
#csv_out << h
# OR use map
#newrow = row.map(&:capitalize)
#csv_out << h
#OR use each ... Add and end
#newrow.each do |k,v| puts "#{k} is #{v}"
#Lastly, write back the edited , regexed data ..etc to an out file.
#csv_out << newrow
end
# close the file
csv_out.close
The output file has the desired data:
USER#USER-SVE1411EGXB:~/Desktop/csvfile$ ls
FL_insurance_sample.csv FL_insurance_sample_out.csv read_write_csv.rb
The input file data looked like this:
policyID,statecode,county,eq_site_limit,hu_site_limit,fl_site_limit,fr_site_limit,tiv_2011,tiv_2012,eq_site_deductible,hu_site_deductible,fl_site_deductible,fr_site_deductible,point_latitude,point_longitude,line,construction,point_granularity
119736,FL,CLAY COUNTY,498960,498960,498960,498960,498960,792148.9,0,9979.2,0,0,30.102261,-81.711777,Residential,Masonry,1
448094,FL,CLAY COUNTY,1322376.3,1322376.3,1322376.3,1322376.3,1322376.3,1438163.57,0,0,0,0,30.063936,-81.707664,Residential,Masonry,3
206893,FL,CLAY COUNTY,190724.4,190724.4,190724.4,190724.4,190724.4,192476.78,0,0,0,0,30.089579,-81.700455,Residential,Wood,1
333743,FL,CLAY COUNTY,0,79520.76,0,0,79520.76,86854.48,0,0,0,0,30.063236,-81.707703,Residential,Wood,3
172534,FL,CLAY COUNTY,0,254281.5,0,254281.5,254281.5,246144.49,0,0,0,0,30.060614,-81.702675,Residential,Wood,1

Reading every line in a CSV and using it to query an API

I have the following Ruby code:
require 'octokit.rb'
require 'csv.rb'
CSV.foreach("actors.csv") do |row|
CSV.open("node_attributes.csv", "wb") do |csv|
csv << [Octokit.user "userid"]
end
end
I have a csv called actors.csv where every row has one entry - a string with a userid.
I want to go through all the rows, and for each row do Octokit.user "userid", and then store the output from each query on a separate row in a CSV - node_attributes.csv.
My code does not seem to do this? How can I modify it to make this work?
require 'csv'
DOC = 'actors.csv'
DOD = 'new_output.csv'
holder = CSV.read(DOC)
You can navigate it by calling
holder[0][0]
=> data in the array
holder[1][0]
=> moar data in array
make sense?
#make this a loop
profile = []
profile[0] = holder[0][0]
profile[1] = holder[1][0]
profile[2] = 'whatever it is you want to store in the new cell'
CSV.open(DOD, "a") do |data|
data << profile.map
end
#end the loop here
That last bit of code will print whatever you want into a new csv file

Convert Hash to csv file

I have the following hash named fcs, and want to save it to a CSV.
{
"bla"=>{
"lower"=>[17.12241, 18.79847, 17.71413, 18.49174, 18.30381, 18.78557, 18.93176, 19.33453, 19.62619, 20.02301],
"point"=>[21.84838, 23.86319, 23.93176, 25.19658, 25.72613, 26.70761, 27.41132, 28.28576, 29.05525, 29.88925],
"upper"=>[26.57434, 28.92791, 30.14938, 31.90142, 33.14845, 34.62966, 35.89088, 37.23698, 38.48432, 39.7555]
},
"blo"=>{
"lower"=>[17.12241, 18.79847, 17.71413, 18.49174, 18.30381, 18.78557, 18.93176, 19.33453, 19.62619, 20.02301],
"point"=>[21.84838, 23.86319, 23.93176, 25.19658, 25.72613, 26.70761, 27.41132, 28.28576, 29.05525, 29.88925],
"upper"=>[26.57434, 28.92791, 30.14938, 31.90142, 33.14845, 34.62966, 35.89088, 37.23698, 38.48432, 39.7555]
}
}
This is the desired content of the output file:
lower.bla,bla,upper.bla,lower.blo,blo,upper.blo
17.12241,21.84838,26.57434,17.12241,21.84838,26.57434
18.79847,23.86319,28.92791,18.79847,23.86319,28.92791
.
.
.
I am using the following code to generate it:
def to_csv
fcs = self.fcs
CSV.generate(col_sep: self.delim) do |csv|
names = Array.new
cols = Array.new
fcs.keys.each do |ts|
names << "lower." + ts
names << ts
names << "upper." + ts
end
csv << names
fcs.values.each do |ts|
cols << ts.values
end
cols.flatten(1).transpose.each do |row|
csv << row
end
end
end
However it uses three different loops. I'm feeling this is not the best code to achieve the desired output. Is there a best way to rewrite it?
header, body =
fcs
.flat_map{|k1, h| h.map{|k2, v| ["#{k2}.#{k1}".sub(/point\./, ""), v]}}
.transpose
[header, *body.transpose].map{|e| e.join(",")}.join($/)
gives:
lower.bla,bla,upper.bla,lower.blo,blo,upper.blo
17.12241,21.84838,26.57434,17.12241,21.84838,26.57434
18.79847,23.86319,28.92791,18.79847,23.86319,28.92791
17.71413,23.93176,30.14938,17.71413,23.93176,30.14938
18.49174,25.19658,31.90142,18.49174,25.19658,31.90142
18.30381,25.72613,33.14845,18.30381,25.72613,33.14845
18.78557,26.70761,34.62966,18.78557,26.70761,34.62966
18.93176,27.41132,35.89088,18.93176,27.41132,35.89088
19.33453,28.28576,37.23698,19.33453,28.28576,37.23698
19.62619,29.05525,38.48432,19.62619,29.05525,38.48432
20.02301,29.88925,39.7555,20.02301,29.88925,39.7555

Resources