如何在Ruby中拆分单元格数组?

时间:2017-05-25 12:21:13

标签: ruby csv parsing nokogiri

代码:

doc = Nokogiri::HTML(html)
showings = []
doc.css('.ok-product').each do |showing|
  showing_id = showing['data-cart-id'].to_i 
  price = showing.at_css('.ok-product__price-main').text.gsub(/[\u0440\u0443\u0431.]/, '').strip
  showings.push(
    id: showing_id,
    price: price
  )
end

CSV.open("file.csv", "wb") do |csv|
  csv << showings
end

我在单元格A1中获取csv中的数据:

{:id=>26999, :price=>"395,00"},"{:id=>26963, :price=>""254,00""}"...

需要将数据分解为单元格并删除不必要的符号。

1 个答案:

答案 0 :(得分:2)

CSV.open("file.csv", "wb") do |csv|
  showings.each do |id_price|
    csv << [id_price[:id], id_price[:price]]
  end
end