python - 如何从python中的字典键中删除点
问题描述
我有python字典如下:
data=df.to_dict('records')
print(data)
给出输出:
[{'id':'1','quantity.':2,'price':'350'},{'id':'2','quantity.':1,'price':'125'},
{'id':'3','quantity.':4,'price':'500'}]
但是在使用命令插入 mongoDBcollection.insert_many(data)
时,会出现如下错误:
bson.errors.InvalidDocument: key 'quantity.' must not contain '.'
由于我们无法在 MongoDB 中存储带有特殊字符的数据,所以我怎样才能删除这个点 '.' 来自所有键,这样我就可以在 MongoDB 中插入数据
解决方案
这种理解应该可以做到。
data = [{k.replace('.', ''): v for k, v in d.items()} for d in data]
推荐阅读
- python - numpy索引ndarray[(4, 2), (5, 3)]的解释
- flutter - 如何仅在一页上阻止屏幕截图?
- django - 是否可以为包含不同模型的两个 Django 项目使用相同的数据库?
- ios - 如何快速创建匿名列表,如 Kotlin
- r - ggplot2中的线条颜色
- r - 在 r 中使用 if else 语句
- php - CakePHP 3.9.4:在组件中使用模型的方法
- android - 如何在 android 中为 AccountPicker 添加自定义对话框?
- azure-devops - 新的管道体验 (YAML) 缺少跟踪和统计功能
- ios - 目标 c - 使用 device_id 预授权请求正文