我在mac os上運行python 2.7 x 10.6,utf8中的文件和utf8中的終端。使用unicode字符在字符串上迭代時字符串字符被錯誤解釋
我想在給定字符串中存在的元音å,ä或ö的每次出現之後添加句點。
這裏是什麼,我試圖做的簡單化版本:
# coding: utf8
a = 'change these letters äöå'
b = map((lambda x: a.replace(x, "{0}.".format(x))), 'åäö')
for c in b:
print c
這procudes以下的輸出:
change these letters ?.??.??.?
change these letters äöå.
change these letters ?.??.??.?
change these letters ä.öå
change these letters ?.??.??.?
change these letters äö.å
爲什麼我得到問號行?經過進一步的研究,只是這樣做會產生相同的問號。
# coding: utf8
for letter in 'åäö':
print letter
輸出:
?
?
?
?
?
?
但在此之前明確地添加的U給
# coding: utf8
for letter in u'åäö':
print letter
輸出:
å
ä
ö
解碼和編碼明確回字符串utf8
仍然產生問號。這裏有什麼問題?什麼是這個循環?
附註:在愚蠢的例子中,你看到了我想要做的。實際上,我正在使用保存字符串的對象,以便映射的操作發生在同一個字符串上。因此,map()
調用實際上每次調用一個新元音的對象方法,從而更新保存在對象中的字符串。該對象的方法使用第二個參數map
中的元音執行替換,並更新存儲的字符串。
我唯一沒有嘗試過的,包含每個元音分開的列表。非常感謝你! – Parham