为了账号安全,请及时绑定邮箱和手机立即绑定

如何解密字符串Froom TextBuffer.get_text

如何解密字符串Froom TextBuffer.get_text

温温酱 2021-03-19 06:30:44
我用pyDes编码了一个char'a',我想对其进行解码   text = self.textbuffer.get_text(start, end)   print text   //',\xcc\x08\xe5\xa1\xa1fc'   x = "{}".format(text)   print x   //',\xcc\x08\xe5\xa1\xa1fc'but i need    //,塡fc当我做cipher_text = ',\xcc\x08\xe5\xa1\xa1fc'print cipher_text//,塡fc为什么    text = self.textbuffer.get_text(start, end)didn't return me a good string ? 您的解决方案在这里不起作用,但我取得了进展:text = self.textbuffer.get_text(start, end)a = text.decode('unicode-escape')g = a.encode('utf-16be')这几乎是很好,但是当我这样做时print g//',���fc'print "%r"%g//"\x00'\x00,\x00\xcc\x00\x08\x00\xe5\x00\xa1\x00\xa1\x00f\x00c\x00'"现在我在如何删除所有\ x00上遇到了问题newstr = g.replace("\x00", "")newstr2 = newstr.replace("'", "")newstr2这是一个不好的解决方案,它仅适用于小字符串
查看完整描述

2 回答

?
www说

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)使事情的痛苦减轻了很多。

查看完整回答
反对 回复 2021-03-31
  • 2 回答
  • 0 关注
  • 176 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号