数据库设置编码为:utf8mb4_unicode_ci 在业务上用数据库已经存储带空格的字符去对比真实输入带有空格的字符串数据,就会遇到表面上看过去一模一样,经过程序对比却是返回False 例子: 数据库可视化查询结果如下:
用程序查询出来的结果:
>>> data [('Free\xa0Pray')]这显然无法正常匹配的上,用unicodedata库即可解决该问题。 解决方案:
new_str = unicodedata.normalize("NFKD", unicode_str) >>> input_name 'Free Pray' >>> db_name 'Free\xa0Pray' >>> input_name == db_name False >>> import unicodedata >>> new_name = unicodedata.normalize("NFKD", db_name) >>> new_name 'Free Pray' >>> new_name == input_name True详情可参考官方文档:https://docs.python.org/2/library/unicodedata.html#unicodedata.normalize
如果觉得文章对您有帮助,欢迎点赞,收藏、关注!
