Lazarix
Lazarix

Reputation: 91

Ruby transpose csv

I'm using ruby and the gem nokogiri to convert html to csv and it's working great, but I would like to transpose the data also.

I understand that ruby has a transpose function that I can leverage but I'm not sure how to integrate it into the script that I'm using as I'm not very good with ruby.

Could someone help me putting the transpose code in the right place in this script to get the desired effect?

require 'rubygems'
require 'nokogiri'

print_header_lines = ARGV[1]

File.open(ARGV[0]) do |f|

table_string=f
doc = Nokogiri::HTML(table_string)

doc.xpath('//table//tr').each do |row|
if print_header_lines
  row.xpath('th').each do |cell|
    print '"', cell.text.gsub("\n", ' ').gsub('"', '\"').gsub(/(\s){2,}/m, '\1'), "\", "
  end
end
row.xpath('td').each do |cell|
  print '"', cell.text.gsub("\n", ' ').gsub('"', '\"').gsub(/(\s){2,}/m, '\1'), "\", "
end
print "\n"
end
end

The script to transpose looks like this according to information I've found online:

require 'CSV'

rows = CSV.new($stdin).read
puts rows.transpose.map { |x| x.join ',' }

Naturally the require will need to go at the top, but I'm unsure what to change in the above example in order to get the output transposed.

Many thanks in advance.

Upvotes: 2

Views: 1206

Answers (1)

seph
seph

Reputation: 6076

You need a 2d array if you want to use transpose. Currently you are just printing it out one line at a time. Use map for this:

tbl_array = doc.xpath('//table//tr').map do |row|
  row.xpath('td').map do |cell|
    "\" #{cell_text.gsub("\n", ' ').gsub('"', '\"').gsub(/(\s){2,}/m, '\1')} \", "
  end
end

tbl_array.transpose.each do |row|
  row.each { |cell| print cell }
end

Upvotes: 2

Related Questions