I have python dictionary as follows:
data=df.to_dict('records')
print(data)
Gives Output:
[{'id':'1','quantity.':2,'price':'350'},{'id':'2','quantity.':1,'price':'125'},
{'id':'3','quantity.':4,'price':'500'}]
But while inserting in mongoDB using command collection.insert_many(data)
it gives error as follows:
bson.errors.InvalidDocument: key 'quantity.' must not contain '.'
As we cannot store data with special characters in MongoDB, So how can I remove this dot '.' from all keys, So that i can insert data in MongoDB
This comprehension ought to do it.
data = [{k.replace('.', ''): v for k, v in d.items()} for d in data]
If you are using pandas, try:
data=data.rename({'quantity.':'quantity'}, axis='columns')
print(data)
In case of you using pandas, you can rename the specific column:
data = data.rename({'quantity.':'quantity'})
Or you can reassign the whole columns name with new one:
data.columns = ['id', 'quantity', 'price']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.