【问题标题】:How to convert unicode accented characters to pure ascii without accents?如何将 unicode 重音字符转换为没有重音的纯 ascii?
【发布时间】:2012-12-16 14:26:43
【问题描述】:

我正在尝试从 http://dictionary.reference.com/browse/apple?s=t 等字典网站下载一些内容

我遇到的问题是原始段落有所有那些波浪线和反向字母等,所以当我阅读本地文件时,我最终会得到那些有趣的转义字符,如 \x85、\xa7、\ x8d等

我的问题是,有什么办法可以将所有这些转义字符转换为它们各自的 UTF-8 字符,例如,如果有一个 'à' 我如何将它转换成一个标准的 'a' ?

Python调用代码:

import os
word = 'apple'
os.system(r'wget.lnk --directory-prefix=G:/projects/words/dictionary/urls/ --output-document=G:\projects\words\dictionary\urls/' + word + '-dict.html http://dictionary.reference.com/browse/' + word)

我在 Windows 7 系统上使用 wget-1.11.4-1(不要杀死我的 Linux 人,这是客户端要求),并且 wget exe 正在使用 Python 2.6 脚本文件启动。

【问题讨论】:

标签: python unicode wget unicode-normalization


【解决方案1】:

我如何将所有这些转义字符转换为它们各自的字符,例如如果有 unicode à,我如何将其转换为标准 a

假设您已将 unicode 加载到名为 my_unicode 的变量中...将 à 规范化为 a 就是这么简单...

import unicodedata
output = unicodedata.normalize('NFD', my_unicode).encode('ascii', 'ignore')

显式示例...

>>> myfoo = u'àà'
>>> myfoo
u'\xe0\xe0'
>>> unicodedata.normalize('NFD', myfoo).encode('ascii', 'ignore')
'aa'
>>>

工作原理
unicodedata.normalize('NFD', "insert-unicode-text-here") 执行 unicode 文本的 Canonical Decomposition (NFD);然后我们使用str.encode('ascii', 'ignore') 将 NFD 映射的字符转换为 ascii(忽略错误)。

【讨论】:

  • 迈克太棒了。这可能是一个 nooby python 问题,但是,是否可以插入一个字符串,并让 unicodedata.norm 函数找到任何 unicode 转义字符并将它们标准化?还是我只需要正则表达式 unicode 并标准化每个?
  • 当你像我上面那样调用unicodedata.normalize()时,它会找到所有的unicode并将它们规范化为ASCII。您需要做的就是将 unicode 文件读入字符串,在该字符串上调用 unicodedata.normalize(),然后将输出保存为新文件名。
  • 其实unicodedata.normalize()不会把字符串转换成ASCII;它执行规范分解(基本上将多部分字符分解为组件);见docs (Python 3.6)str.encode('ascii', 'ignore') 函数转换为 ASCII,忽略非 ASCII 字符可能出现的错误。请参阅 str.encodeerror handlers 上的文档。
  • 感谢您纠正我上面的评论。我冒昧地将这些信息编辑到我的答案中。
【解决方案2】:

我需要这样的东西,但要只删除重音字符,忽略特殊字符,我做了这个小功能:

# ~*~ coding: utf-8 ~*~
import re

def remove_accents(string):
    if type(string) is not unicode:
        string = unicode(string, encoding='utf-8')

    string = re.sub(u"[àáâãäå]", 'a', string)
    string = re.sub(u"[èéêë]", 'e', string)
    string = re.sub(u"[ìíîï]", 'i', string)
    string = re.sub(u"[òóôõö]", 'o', string)
    string = re.sub(u"[ùúûü]", 'u', string)
    string = re.sub(u"[ýÿ]", 'y', string)

    return string

我喜欢这个功能,因为你可以自定义它以防你需要忽略其他字符

【讨论】:

  • SyntaxError: Non-ASCII character '\xc3' in file source.py on line 65, but no encoding declaration;详情见python.org/peps/pep-0263.html
  • 需要广告到文件开头:# -- coding: utf-8 --
【解决方案3】:

@Mike Pennington 的解决方案非常感谢他。但是当我尝试该解决方案时,我注意到它没有在 NFD 中定义的一些特殊字符(即土耳其字母表中的 ı 字符)失败。

我发现了另一种解决方案,您可以使用 unidecode 库进行此转换。

>>>import unidecode
>>>example = "ABCÇDEFGĞHIİJKLMNOÖPRSŞTUÜVYZabcçdefgğhıijklmnoöprsştuüvyz"


#convert it to utf-8
>>>utf8text = unicode(example, "utf-8")

>>> print utf8text
ABCÇDEFGĞHIİJKLMNOÖPRSŞTUÜVYZabcçdefgğhıijklmnoöprsştuüvyz

#convert utf-8 to ascii text
asciitext = unidecode.unidecode(utf8text)

>>>print asciitext

ABCCDEFGGHIIJKLMNOOPRSSTUUVYZabccdefgghiijklmnooprsstuuvyz

【讨论】:

  • 对一般的字符串操作很有帮助。我不明白为什么这个答案的票数这么少......
【解决方案4】:

给定的 URL 返回 UTF-8,因为 HTTP 响应清楚地表明:

wget -S http://dictionary.reference.com/browse/apple?s=t
--2013-01-02 08:43:40--  http://dictionary.reference.com/browse/apple?s=t
Resolving dictionary.reference.com (dictionary.reference.com)... 23.14.94.26, 23.14.94.11
Connecting to dictionary.reference.com (dictionary.reference.com)|23.14.94.26|:80... connected.
HTTP request sent, awaiting response... 
  HTTP/1.1 200 OK
  Server: Apache
  Cache-Control: private
  Content-Type: text/html;charset=UTF-8
  Date: Wed, 02 Jan 2013 07:43:40 GMT
  Transfer-Encoding:  chunked
  Connection: keep-alive
  Connection: Transfer-Encoding
  Set-Cookie: sid=UOPlLC7t-zl20-k7; Domain=reference.com; Expires=Wed, 02-Jan-2013 08:13:40 GMT; Path=/
  Set-Cookie: cu.wz=0; Domain=.reference.com; Expires=Thu, 02-Jan-2014 07:43:40 GMT; Path=/
  Set-Cookie: recsrch=apple; Domain=reference.com; Expires=Tue, 02-Apr-2013 07:43:40 GMT; Path=/
  Set-Cookie: dcc=*~*~*~*~*~*~*~*~; Domain=reference.com; Expires=Thu, 02-Jan-2014 07:43:40 GMT; Path=/
  Set-Cookie: iv_dic=1-0; Domain=reference.com; Expires=Thu, 03-Jan-2013 07:43:40 GMT; Path=/
  Set-Cookie: accepting=1; Domain=.reference.com; Expires=Thu, 02-Jan-2014 07:43:40 GMT; Path=/
  Set-Cookie: bid=UOPlLC7t-zlrHXne; Domain=reference.com; Expires=Fri, 02-Jan-2015 07:43:40 GMT; Path=/
Length: unspecified [text/html]

使用 vim 调查保存的文件还发现数据是正确的 utf-8 编码...使用 Python 获取 URL 也是如此。

【讨论】:

  • 是的,这是真的,但 OP 并不是真的意味着他想将字符转换为 UTF-8。他想把它们转换成 ASCII。
【解决方案5】:

这个问题对我来说是不同的,但这个堆栈页面可以解决它unicodedata.normalize('NFKC', 'V').encode('ascii', 'ignore') 输出 - b'V'

【讨论】:

    猜你喜欢
    • 2012-04-13
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 2012-11-27
    • 1970-01-01
    相关资源
    最近更新 更多