字符串不能写入到CSV文件Ruby

时间:2022-04-19 13:59:57

I don't necessarily understand why this bit of code is incorrect. I understand the error in that string class doesn't have a method map. But I'm having a hard time wrapping my head around this error.

我不明白为什么这段代码不正确。我理解字符串类中的错误没有方法映射。但我正绞尽脑汁地想这个错误。

The error

这个错误

`<<': undefined method `map' for #<String:0x000001020b8940> (NoMethodError)

The but of code

但是,代码

require 'nokogiri'
require 'open-uri'
require 'csv'

doc = Nokogiri::HTML(open("dent-file.html"))

new_array = doc.search("p").map do |para|
     para.text
end

CSV.open("dent.csv", "w") do |csv|
   new_array.each do |string|
     csv << string
   end
end

I want to write each element of the newdoc array to each line of the csv file dent.csv.

我想将newdoc数组的每个元素写入csv文件的每一行。

1 个解决方案

#1


9  

CSV#<< accepts an array or a CSV::Row. Convert the string to an array.

<接受数组或csv::行。将字符串转换为数组。< p>

csv << [string]

#1


9  

CSV#<< accepts an array or a CSV::Row. Convert the string to an array.

<接受数组或csv::行。将字符串转换为数组。< p>

csv << [string]