不可散列类型:在pymongo中使用推送时为"列表"



我目前正在做一个网页抓取项目。目标是检索加油站(900多个)每天可用的不同类型燃料的价格。如果价格发生变化,脚本将能够将新的价格附加到我的Mongodb数据库中。

收集到的数据如下所示:

Price_post_api = {
"station_id": 31200009,
"price_detail": [
{
"fuel_id": 1,
"fuel_name": "Gazole",
"fuel_cost": 1.959,
"update_date": {
"$date": "2022-05-30T10:05:22Z"
}
},
{
"fuel_id": 2,
"fuel_name": "SP95",
"fuel_cost": 2.049,
"update_date": {
"$date": "2022-05-30T10:05:23Z"
}
},
{
"fuel_id": 5,
"fuel_name": "E10",
"fuel_cost": 2.009,
"update_date": {
"$date": "2022-05-30T10:05:23Z"
}
}
]
},

我很难弄清楚如何正确地$push在Mongodb基于"fuel_cost"字段的数据。下面是db.

中预期输出的示例。
Mongodb_price_data ={
"station_id": 31200009,
"price_detail": [
{
"fuel_id": 1,
"fuel_name": "Gazole",
"fuel_cost": 1.959,
"update_date": {
"$date": "2022-05-30T10:05:22Z"
}
},
{
"fuel_id": 1,
"fuel_name": "Gazole",
"fuel_cost": 35.87,
"update_date": {
"$date": "2022-05-31T10:09:22Z"
}
},
{
"fuel_id": 2,
"fuel_name": "SP95",
"fuel_cost": 2.049,
"update_date": {
"$date": "2022-05-30T10:05:23Z"
}
},
{
"fuel_id": 2,
"fuel_name": "Gazole",
"fuel_cost": 1.59,
"update_date": {
"$date": "2022-07-14T00:10:19Z"
}
},
{
"fuel_id": 5,
"fuel_name": "E10",
"fuel_cost": 2.009,
"update_date": {
"$date": "2022-05-30T10:05:23Z"
}
}
]
}
到目前为止,我创建了以下函数:
def update_new_price(station_id, fuel_id, fuel_name, cost):
query = {'station_id':station_id, 'price_detail.fuel_id':fuel_id, 'price_detail.fuel_name':fuel_name,'price_detail.fuel_cost':cost}
new_value = {
'$push':{
'price_detail':{
[{'price_detail.fuel_id': fuel_id,'price_detail.fuel_name':fuel_name ,'price_detail.fuel_cost':cost}]
}}}
result = db[CL_PRICE].find(query)
if not list(result):
db[CL_PRICE].update_one(query,new_value,upsert= True)
print('new value added')
else:
print('Already exists')

一旦我运行我的代码,我得到一个错误消息。

'price_detail':{
TypeError: unhashable type: 'list'

知道为什么和我怎么能解决它吗?

我认为你需要去掉多余的大括号。

不是

'price_detail':{
[{'price_detail.fuel_id': fuel_id,'price_detail.fuel_name':fuel_name ,'price_detail.fuel_cost':cost}]
}

应该是

'price_detail': [
{'price_detail.fuel_id': fuel_id,'price_detail.fuel_name':fuel_name ,'price_detail.fuel_cost':cost}
]

你实际上是在尝试创建一个由单个元素组成的集合,这是一个列表。set只能包含列表不能包含的可哈希对象。

一个最小的可重复的例子是

>>> {[1, 2, 3]}
Traceback (most recent call last):
Input In [2] in <cell line: 1>
{[1, 2, 3]}
TypeError: unhashable type: 'list'

中间没有:{}是创建集合的语法。为了清楚起见,这里有一些创建集合的正确方法的示例。

>>> {(1, 2, 3)}  # a set containing a single tuple
{(1, 2, 3)}
>>> {*[1, 2, 3]}  # a set of three elements created from a list
{1, 2, 3}
>>> {1, 2, 3}  # a set created from three values
{1, 2, 3}
>>> {x for x in range(1, 4)}  # a set comprehension expression
{1, 2, 3}

相关内容

  • 没有找到相关文章

最新更新