如何在Python 3.5中删除重音符号并使用unicodedata或其他解决方案获取字符串? [英] How to remove accent in Python 3.5 and get a string with unicodedata or other solutions?

查看:73
本文介绍了如何在Python 3.5中删除重音符号并使用unicodedata或其他解决方案获取字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Google地理编码api中使用字符串。我检查了很多线程,但仍然遇到问题,我不知道如何解决。

I am trying to get a string to use in google geocoding api.I ve checked a lot of threads but I am still facing problem and I don't understand how to solve it.

我需要addresse1为没有任何特殊字符的字符串。例如, addresse1是: 32 rue d'AthènesParis France。

I need addresse1 to be a string without any special characters. Addresse1 is for example: "32 rue d'Athènes Paris France".

addresse1= collect.replace(' ','+').replace('\n','') 
addresse1=unicodedata.normalize('NFKD', addresse1).encode('utf-8','ignore') 

在这里,我得到了一个没有任何重音符号的字符串。。。不,不是字符串,而是字节。因此,我完成了建议并进行了解码:

here I got a string without any accent... Ho no... It is not a string but a bytes. So I ve done what was suggested and 'decode:

addresse1=addresse1.decode('utf-8') 

但是addresse1与开始时完全相同...我该怎么办?我究竟做错了什么?还是我不了解unicode?还是有更好的解决方案?

But then addresse1 is exactly the same than at the begining... What do I have to do? What am I doing wrong? Or what i don't understand with unicode? Or is there a better solution?

谢谢

Stéphane。

推荐答案

(带有第三方包装): Unidecode

with 3rd party package: unidecode

3>> unidecode.unidecode("32 rue d'Athènes Paris France")
"32 rue d'Athenes Paris France"

这篇关于如何在Python 3.5中删除重音符号并使用unicodedata或其他解决方案获取字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆