wxpython: How do we remove null byte from string when using textcontrol.GetValue()

我与影子孤独终老i 提交于 2019-12-13 18:20:49

问题


Objective: Use two textcontrol boxes, one as an input and one as an output However there is an additional step in the process, which fails likely because textcontrol.GetValue() returns null byte in addition

So say I have

self.tc1 = wx.TextCtrl(panel)

self.tc2 = wx.TextCtrl(panel)

and I go about it

cmd = self.tc1.GetValue()

How do I exclude last character of string cmd (or any null byte)

Output:

TypeError: execv() argument 1 must be encoded string without NULL bytes, not str


回答1:


Just figured it out,

I had to change it to

cmd = self.tc1.GetValue().encode('ascii')



回答2:


You simply use str.strip.

>>> foo = "bar\n"
>>> foo
'bar\n'
>>> foo.strip()
'bar'

Note this also removes any leading and trailing whitespace.



来源:https://stackoverflow.com/questions/15203106/wxpython-how-do-we-remove-null-byte-from-string-when-using-textcontrol-getvalue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!