【问题标题】:How to split an array of cells in Ruby?如何在 Ruby 中拆分单元格数组?
【发布时间】:2017-10-26 02:26:35
【问题描述】:

代码:

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""}"...

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

【问题讨论】:

  • 细胞叫什么?
  • 现在全部在 A1 中。但我需要:A1、B1、A2、B2、A3、B3 ...
  • 请阅读“minimal reproducible example”。我们需要证明问题所需的最少输入数据。 html 是什么?没有这个,你的问题就没有多大意义。

标签: ruby csv parsing nokogiri


【解决方案1】:
CSV.open("file.csv", "wb") do |csv|
  showings.each do |id_price|
    csv << [id_price[:id], id_price[:price]]
  end
end

【讨论】:

  • 谢谢。如果在单元格 A1 和 B1 中?
  • 也许举个例子(在 csv 中)数据应该是什么样子会有所帮助,@Mamom
  • 我在 csv 中做了一个脚本
  • 添加标题行以供稍后解析很有用,允许您将列视为哈希值(或阅读/测试时的清晰度)。 (; = 换行符) 例如CSV.open("file.csv", "wb") do |csv| ; csv &lt;&lt; "showing_id, price" ; showings.each do |showing| ; csv &lt;&lt; [showing[:id], showing[:price]] ; end ; end ;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-08
相关资源
最近更新 更多