2 回答

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個(gè)贊
您最好使用新的字符串格式設(shè)置系統(tǒng):
>>> cipher_text = ',\xcc\x08\xe5\xa1\xa1fc'
>>> print cipher_text
,塡fc
>>> print "%r" % cipher_text
',\xcc\x08\xe5\xa1\xa1fc'
>>> print "{}".format(cipher_text)
,塡fc
>>> p = "%r" % cipher_text
>>> print p
',\xcc\x08\xe5\xa1\xa1fc'
>>> p = "{}".format(cipher_text)
>>> print p
,塡fc
看起來舊的格式化字符串的方法似乎存在嚴(yán)重的unicode和ascii問題(這是我在嘗試時(shí)發(fā)現(xiàn)的問題),而新的格式化系統(tǒng)卻像一個(gè)魅力。此外,它已經(jīng)為python3準(zhǔn)備好了!
在將更多詳細(xì)信息添加到問題后進(jìn)行編輯:
afaict,gtk在處理unicode字符串時(shí)沒有問題。您應(yīng)該從TextBuffer.get_text()中獲得一個(gè)unicode字符串。因此,為了確定我的假設(shè),您應(yīng)該首先執(zhí)行以下操作:
print type(text)
查看TextBuffer是否返回str()或unicode()對(duì)象。
然后,您可以嘗試
text = unicode(self.textbuffer.get_text(start, end)
或者
text = self.textbuffer.get_text(start, end).encode('utf-8')
甚至
text = '{}'.format(self.textbuffer.get_text(start_end))
在python中在utf-8和ascii之間轉(zhuǎn)換時(shí),事情通常會(huì)變得棘手。關(guān)于該主題有一個(gè)很好的手冊(cè),使用python3(默認(rèn)情況下使用unicode)使事情的痛苦減輕了很多。
添加回答
舉報(bào)