python - 如何修复 "' str' object does not support item assignment"在 MongoDb 中的错误?

标签 python json mongodb pymongo

我有这样一个字符串:

document = '{ time : 14/09/19 16:00:00,
   global : { full:190 , defects: 7 , btp: 6 , total: 202 } ,
   domains : [ { domain : "A" , full:7 , defects: 2 , btp: 0 , total: 9 },
               { domain : "B" , full:0 , defects: 0 , btp: 0 , total: 0 },
               { domain : "C" , full:6 , defects: 0 , btp: 0 , total: 6 },
               { domain : "D" , full:26 , defects: 0 , btp: 2 , total: 28 },
               { domain : "E" , full:0 , defects: 0 , btp: 0 , total: 0 },
               { domain : "F" , full:4 , defects: 0 , btp: 2 , total: 6 },
               { domain : "G" , full:0 , defects: 0 , btp: 0 , total: 0 },
               { domain : "H" , full:21 , defects: 0 , btp: 1 , total: 22 },
               { domain : "I" , full:32 , defects: 0 , btp: 0 , total: 32 },
               ] }'

当我尝试将其插入到 mongoDB 集合中时,出现以下错误:

TypeError: 'str' object does not support item assignment

我不知道如何将字符串转换为 BSON,以便用它执行插入。

我查看了 bson_util 模块,但没有成功。

最佳答案

这里重要的不是 BSON 部分。 MongoDB 驱动程序会处理这些。您的工作是将 JSON 转换为有效的 Python 数据结构:

import json

data = json.loads(document)

collection.insert(data);

关于python - 如何修复 "' str' object does not support item assignment"在 MongoDb 中的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28898476/

相关文章:

python - 是什么让用户定义的对象不会在 Python 中抛出 AttributeError?

python - 使用Python将大文件发布到heroku

json - 去 json.Unmarshal 不工作

python - 停止 Twisted 吞下异常

java - Jackson 中的通用元组反序列化

java - 使用 Spring 将嵌套集合中的对象转换为 JSON

javascript - 在 Mongoose 中聚合时无法使用 moment.js

node.js - 检查数组中是否存在所有元素

mongodb - Mongo 2.6 的 meteor oplog

python - 如何在 Python 中使用 OSMnx 填充水体