如何安全地解码 wxPython 应用程序中的度数符号?
我有一个我一直在编写的调试应用程序,它通过 UDP 从基于 C 的进程接收数据。 发送给我的字符串之一包含一个 °
字符 - Unicode U+00B0< /a> (这顺便破坏了 StackOverflow 搜索功能!)。 当我的 wxPython 应用程序尝试将该字符串附加到文本框时,我收到一个 UnicodeDecodeError
。
我第一次尝试解决这个问题只是发现了这个错误(因为该应用程序显然确实发送了一些坏消息。问题是该应用程序还使用该字符来报告设备周围的各种温度,这就是我们所要做的确实需要记录。更改源应用程序超出了我的控制范围,那么我如何检测这些符号并将其解码为 wxTextCtrl
可以显示的内容?
I have a debug app I've been writing which receives data from a C-based process via UDP. One of the strings sent to me contains a °
character - Unicode U+00B0 (which incidentally breaks the StackOverflow search function!). When my wxPython application tries to append that string to a text box I get a UnicodeDecodeError
.
My first attempt to fix the issue simply caught that error (because the app apparently does send some bad messages. The problem is that the app also uses the character to report various temperatures around the unit and that's something we really need to log. Changing the source app is out of my control, so how can I detect and decode those symbols into something the wxTextCtrl
can display?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
pdc 做对了,以下工作正常(但在没有
解码
的情况下失败):pdc got it right, the following works fine (but fails without the
decode
):我不能对 wxPython 本身说 mych,但我猜测它在显示之前尝试将文本转换为 Unicode,如果你有一个像
'123\xB0'
这样的字符串并尝试转换使用默认编码 (ASCII) 将其转换为 Unicode,然后它将抛出UnicodeDecodeError
。 您可以通过替换来解决此问题(
其中我假设函数
get_string()
获取字符串形式的消息)。 此处的区别在于,通过自行转换为 Unicode,您可以指定编码。I can't say mych about wxPython itself, but I am guessing that it is trying to convert the text to Unicode before displaying it, If you have a string like
'123\xB0'
and try to convert it to Unicode with teh default encoding (ASCII) then it will throwUnicodeDecodeError
. You can probably fix this by replacingwith
(where I am assuming a function
get_string()
that gets the message as a string). The difference here is that by handong the conversion to Unicode yourself you get to specify the encoding.当这个问题被问到时,情况可能有所不同,但我对任何偶然发现这个问题的人的想法是:
问题是 wxPython 正在尝试转换为 unicode,并且缺乏字符集信息,它尝试使用 ASCII,这是无效的。 如果您知道您的数据是 utf-8,请告诉它,它就会正常工作。
Things may have been different back when this was asked, but my thoughts for anyone who stumbles on this:
The issue is wxPython is trying to convert TO unicode, and lacking charset information it tries to use ASCII, which is invalid. If you know your data is utf-8, tell it so and it'll just work.