2011-02-08 137 views
1

我從其MP3標籤中讀取歌曲的藝術家,然後根據該名稱創建一個文件夾。我遇到的問題是名稱包含特殊字符(如「AC \ DC」)。所以我編寫了這個代碼來處理這個問題。python:替換字符串中的特殊字符

def replace_all(text): 
    print "replace_all" 
    dictionary = {'\\':"", '?':"", '/':"", '...':"", ':':"", chr(148):"o"} 

    for i, j in dictionary.iteritems(): 
     text = text.replace(i,j) 

    return text 

我現在遇到的是如何處理非英語字符,如Motorhead或Blue Oyster邪教組織中的umlaout o。

正如你看到的我試過在字典的末尾添加umlaout 0的ASCII字符串版本,但未能與

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4: ordinal not in range(128) 
+1

http://farmdev.com/talks/unicode/ – 2011-02-08 11:36:08

+0

另請參閱http://stackoverflow.com/questions/3833791/python-regex-to-convert-non-ascii-characters-in-a-string-爲了討論更一般的解決方案,最接近的ascii-equival。 – Mikel 2011-02-08 11:45:36

+0

@Ignacio這是一個很棒的鏈接!它幫助了很多。 – ccwhite1 2011-02-08 15:13:44

回答

3

我發現這段代碼,但我不明白。

def strip_accents(s): 
    return ''.join((c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn')) 

它使我能夠從建議的目錄/文件名的路徑中刪除重音符號。

0

我建議使用Unicode用於輸入文本和字符替換。在你的例子中,chr(148)顯然不是一個unicode符號。