2 回答

TA贡献1775条经验 获得超8个赞
您最好使用新的字符串格式设置系统:
>>> 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
看起来旧的格式化字符串的方法似乎存在严重的unicode和ascii问题(这是我在尝试时发现的问题),而新的格式化系统却像一个魅力。此外,它已经为python3准备好了!
在将更多详细信息添加到问题后进行编辑:
afaict,gtk在处理unicode字符串时没有问题。您应该从TextBuffer.get_text()中获得一个unicode字符串。因此,为了确定我的假设,您应该首先执行以下操作:
print type(text)
查看TextBuffer是否返回str()或unicode()对象。
然后,您可以尝试
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之间转换时,事情通常会变得棘手。关于该主题有一个很好的手册,使用python3(默认情况下使用unicode)使事情的痛苦减轻了很多。
添加回答
举报