TypeError:无法将数组转换为字符串
Posted
技术标签:
【中文标题】TypeError:无法将数组转换为字符串【英文标题】:TypeError: can't cast Array to string 【发布时间】:2014-10-24 12:06:21 【问题描述】:我在尝试使用 Nokogiri 运行 rake 任务时遇到此错误。
这是代码。
namespace :tops do
desc "Get Tops Description"
task get_description: :environment do
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::html(open("http://ceratoboutique.com/collections/tops"))
price = Array.new
items = Array.new
brands = Array.new
sale_price = Array.new
image = Array.new
item_url = Array.new
price = doc.xpath("//del").collect |node| node.text.strip
items = doc.xpath("//div/a/h4").collect |node| node.text.strip
brands = doc.xpath("//span[contains(@class,'vendor')]").collect |node| node.text.strip
sale_price = doc.xpath("//span[contains(@class, 'price')]/text()").collect |node| node.text.strip
image = doc.xpath("/div/a/img/@src").collect |node| node.text.strip
item_url = doc.css('div.details a').map |link| link['href'] .collect
price.each do |prices|
Tops.create(description: price)
end
end
end
这是完整的错误。
Rake 中止! /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/quoting.rb:76:in
type_cast' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/sqlite3_adapter.rb:261:in
type_cast' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/sqlite3_adapter.rb:295:inblock in exec_query' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/sqlite3_adapter.rb:294:in
map' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/sqlite3_adapter.rb:294:inexec_query' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:68:in
exec_insert' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:95:ininsert' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in
插入' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/relation.rb:64:ininsert' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/persistence.rb:502:in
_create_record' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/attribute_methods/dirty.rb:87:in_create_record' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/callbacks.rb:306:in
block in _create_record' /Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.5/lib/active_support/callbacks.rb:82:inrun_callbacks' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/callbacks.rb:306:in
_create_record' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/timestamp.rb:57:in_create_record' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/persistence.rb:482:in
create_or_update' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/callbacks.rb:302:inblock in create_or_update' /Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.5/lib/active_support/callbacks.rb:82:in
run_callbacks' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/callbacks.rb:302:increate_or_update' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/persistence.rb:103:in
save' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/validations.rb:51:insave' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/attribute_methods/dirty.rb:21:in
save' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:268:inblock (2 levels) in save' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:329:in
block in with_transaction_returning_status' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:201:inblock in transaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:209:in
within_new_transaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:201:intransaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:208:in
transaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:326:inwith_transaction_returning_status' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:268:in
block in save' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:283:inrollback_active_record_state!' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:267:in
save' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/persistence.rb:34:increate' /Users/Kcummings/Sites/rvm_tests/lib/tasks/tops.rake:24:in
block (3 级) in ' /Users/Kcummings/Sites/rvm_tests/lib/tasks/tops.rake:23:ineach' /Users/Kcummings/Sites/rvm_tests/lib/tasks/tops.rake:23:in
block (2 水平)在' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:240:incall' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:240:in
block 正在执行' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:235:ineach' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:235:in
执行' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:179:inblock in invoke_with_call_chain' /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/monitor.rb:211:in
mon_synchronize' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:172:ininvoke_with_call_chain' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:165:in
invoke' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:150:ininvoke_task' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:106:in
block (2 个级别) in top_level' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:106:ineach' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:106:in
block in top_level' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:115:inrun_with_threads' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:100:in
top_level' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:78:inblock in run' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:176:in
standard_exception_handling' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:75:inrun' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/bin/rake:33:in
' /usr/bin/rake:23:inload' /usr/bin/rake:23:in
'
编辑:TOPS TABLE SCHEMA
ActiveRecord::Schema.define(version: 20140824150641) do
create_table "tops", force: true do |t|
t.string "brand"
t.string "description"
t.string "image"
t.decimal "price"
t.datetime "created_at"
t.datetime "updated_at"
t.string "url"
end
end
【问题讨论】:
发布您的tops
表架构
【参考方案1】:
似乎您在表tops
中有description
字段为'varchar' 或string
,但是您正在尝试在其中存储数组。
您需要将description
字段的数据类型更改为text
,然后将值更改为serialize
,然后再尝试存储它。
生成新的迁移文件并在终端上运行它:
rails g migration change_desc_in_my_tops
当您使用 Rails 4 时,请在迁移文件中添加以下代码:
class ChangeDescInTops < ActiveRecord::Migration
def up
change_column :tops, :description, :text
end
def down
change_column :tops, :description, :string
end
end
现在运行迁移:
rake db:migrate
将您的 tops
模型更新为:
class Tops < ActiveModel::Base
serialize :description, Array
...
...
end
现在尝试运行您的 rake 任务。希望对你有帮助。
注意:您应该使用单数型号名称。您的Tops
模型可能是Top
。它在使用关联时避免了混淆,并且遵循约定。
【讨论】:
太棒了!非常感谢【参考方案2】:您将price
设置为一个数组(而不是称其为prices
),这可能是您下面的错字的一个促成因素
price.each do |prices|
Tops.create(description: price)
end
您正在分配您正在迭代的数组,而不是正在生成的单个元素。
【讨论】:
以上是关于TypeError:无法将数组转换为字符串的主要内容,如果未能解决你的问题,请参考以下文章
GPT 2 - TypeError:无法根据规则“安全”将数组数据从 dtype('O') 转换为 dtype('int64')
渲染错误:“ TypeError:无法读取null的属性'PAY_TYPE'”
TypeError:在绘制 seaborn.regplot 时,无法根据规则“安全”将数组数据从 dtype('int64') 转换为 dtype('int32')
在 tensorflow 2.4 中使用 sampled_softmax 时无法将符号 Keras 输入/输出转换为 numpy 数组 TypeError
TypeError:无法将 Net::HTTPOK 转换为字符串
TypeError:获取参数数组的类型无效 numpy.ndarray,必须是字符串或张量。 (不能将 ndarray 转换为张量或操作。)