2017-05-12 88 views

回答

0

哪個Python版本是什麼呢?在Python 3,使用lower()正確轉換它:

>>> x = 'LÄCHERLICH' 
>>> print(x.lower()) 
lächerlich 

使用Python 2,你應該使用Unicode字符串(不要忘記定義在文件的最開始編碼):

# coding: utf-8 
x = u'LÄCHERLICH' 
print x.lower().encode('utf8') 
+1

這不是問題的答案,應該是註釋 – Craicerjack

+0

我正在使用2.7版本 – Shan

0

這應做到:

# -*- coding: utf-8 -*- 
a = 'LÄCHERLICH' 
print a.decode('utf8').lower() 

decode將工作,如果你想在u'LÄCHERLICH'使用lower()

0

對於Python 2.7

的問題是,當你在聲明字符串它採取爲ASCII你必須定義它在UTF在聲明或以後的時間。

In [17]: str = 'LÄCHERLICH' # didn't specify encoding(so ASCII by default) 

In [18]: print str.lower() 
lÄcherlich 

In [19]: str = u'LÄCHERLICH' #declaring that it's UTF 

In [20]: print str.lower() 
lächerlich 

聲明後,將其轉換:

In [21]: str = 'LÄCHERLICH' 

In [22]: print str.decode('utf8').lower() 
lächerlich 
相關問題