[英]How to convert unicode accented characters to pure ascii without accents?
我正在嘗試從像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 腳本文件被觸發。
我如何將所有這些轉義字符轉換為它們各自的字符,例如如果有一個 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 文本的規范分解 (NFD) ; 然后我們使用str.encode('ascii', 'ignore')
將 NFD 映射的字符轉換為 ascii(忽略錯誤)。
我需要這樣的東西,但只刪除重音字符,忽略特殊字符,我做了這個小功能:
# ~*~ 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
我喜歡這個功能,因為你可以自定義它以防你需要忽略其他字符
給定的 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 也是如此。
感謝他,@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
這個問題對我來說是不同的,但是這個堆棧頁面可以解決它unicodedata.normalize('NFKC', 'V').encode('ascii', 'ignore')
output - b'V'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.