如何解密字符串Froom TextBuffer.get_text

我用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这是一个不好的解决方案,它仅适用于小字符串


温温酱
浏览 177回答 2
2回答

www说

您最好使用新的字符串格式设置系统:>>> 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)使事情的痛苦减轻了很多。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python