在python中大约将unicode字符串转换为ASCII字符串 [英] Approximately converting unicode string to ascii string in python
问题描述
不知道这是否很简单,但是我需要将unicode字符串转换为ascii字符串,并且我不希望所有这些转义字符都存在.我的意思是,是否可以将近似"转换为一些非常相似的ascii字符?
don't know wether this is trivial or not, but I'd need to convert an unicode string to ascii string, and I wouldn't like to have all those escape chars around. I mean, is it possible to have an "approximate" conversion to some quite similar ascii character?
例如:加文·奥康纳(Gavin O'Connor)被转换为加文·奥康纳(Gavin O \ x92Connor),但我真的希望它能被转换为加文·奥康纳(Gavin O'Connor).这可能吗?有没有人写一些工具来做到这一点,还是我必须手动替换所有字符?
For example: Gavin O’Connor gets converted to Gavin O\x92Connor, but I'd really like it to be just converted to Gavin O'Connor. Is this possible? Did anyone write some util to do it, or do I have to manually replace all chars?
非常感谢! 马可
推荐答案
使用 Unidecode 包音译字符串.
>>> import unidecode
>>> unidecode.unidecode(u'Gavin O’Connor')
"Gavin O'Connor"
这篇关于在python中大约将unicode字符串转换为ASCII字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!