【发布时间】:2020-06-23 19:25:35
【问题描述】:
我正在使用烧瓶和 pymongo 开展一个项目,并且我有一个用户列表,其中用户实例如下:
user = {"Email":"user@gmail.com" , "Comments":["good" , "not very good " , "hated it "]}
我正在尝试遍历我的用户集合中每个用户的 cmets,如果我在任何评论中找到 string "good",我想用 "bad" 替换它。使用我下面的代码,每次都会替换字符串,但没有保存在我的
用户集合。
user_list = users.find()
for usr in user_list:
for comment in usr['Comments']:
if "good" in comment:
print("old comment here")
print(comment)
comment=comment.replace("good" ,"bad") #does not save the edited comment in the collection !
print(comment) # the new comment is printed
感谢您对这项简单任务的帮助。 提前谢谢你。
【问题讨论】:
-
您正在更改 for 循环中的变量注释,而不是列表中的值。
标签: python string mongodb replace pymongo