python - Mongoengine:匹配重音字符作为基础字符
问题描述
相关:如何在 MongoDB db.foo.find() 语法中执行此操作。
假设我有一个模型
class Foo(Document):
name = StringField()
该集合的数据库状态为:
[{"name":"Jesús"},{"name":"Jesus"}]
我想要一个匹配两个文档的查询,即搜索,但变音符号标准化,例如:
Foo.objects.filter(name__icontains="jesus")
有没有办法直接在mongoengine中做到这一点?
解决方案
使用python,您可以import unidecode
,然后将所有重音符号与普通文本进行比较,
from unidecode import unidecode
li = [ ]
for entry in Foo.objects:
if unidecode(entry.name) == "Jesus":
li.append(entry)
# now li has your filtered entries
或者,你可以做
from unidecode import unidecode
li = [ entry for entry in Foo.objects if unidecode(entry.name) == "Jesus" ]
注意:您必须
unidecode
使用pip install unidecode
.
编辑:以下代码按预期工作,
>>> unidecode('Jesús')
Jesus
推荐阅读
- android - Unity Editor - 坚持使用 IL2CPP 构建本机二进制文件
- html - 溢出问题
- sql-server - 声明标量变量中遗漏了什么
- python - 比较来自表单字段值和查询集值的值
- wordpress - 我将如何将移动用户从 wordpress 重定向到我的 amp 页面以获取该单页?
- html - 如何在html中正确添加空格
- r - R ggplot 热图手动分箱和选择中间箱颜色
- django - 如何将带有上下文变量的 Django 模板发送到 Ajax 调用?
- php - 如何从 wp-admin .htaccess 中删除 www
- python - Python中的正则表达式:在“&”之前找到三个浮点数