2013-10-12 61 views
1

當我嘗試爲QString轉換爲常規的Python字符串我收到此錯誤:UnicodeEncodeError:「ASCII」編解碼器不能編碼字符U「 u2029」

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2029' in position 3: ordeal not in range(128) 

所有我做的是這個:

str(string) 

string是QString,但它然後給我那個錯誤。我怎樣才能解決這個問題?

+1

'str(u'\ u2029'.encode('utf8'))' – Vor

+0

@Vor爲什麼不把它放入答案? – ezdazuzena

回答

1

Python 2.x中的名稱str具有誤導性;由於歷史原因,str is bytes - 字符串而不是字符。如果您嘗試將字符串轉換爲字符串,Python默認使用ASCII。只需使用unicode(string)即可在Python 2.x下獲取字符串,或切換到3.x,其中str實際上是一個字符串類型。

相關問題