如何安全地解码 wxPython 应用程序中的度数符号?

发布于 2024-07-08 20:59:25 字数 468 浏览 9 评论 0原文

我有一个我一直在编写的调试应用程序,它通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪意 2024-07-15 20:59:26

pdc 做对了,以下工作正常(但在没​​有解码的情况下失败):

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wx

app = wx.PySimpleApp()
app.TopWindow = wx.Frame(None)
field = wx.TextCtrl(app.TopWindow)
field.Value += '°'.decode('ISO8859-1')
app.TopWindow.Show()
app.MainLoop()

pdc got it right, the following works fine (but fails without the decode):

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wx

app = wx.PySimpleApp()
app.TopWindow = wx.Frame(None)
field = wx.TextCtrl(app.TopWindow)
field.Value += '°'.decode('ISO8859-1')
app.TopWindow.Show()
app.MainLoop()
你与昨日 2024-07-15 20:59:26

我不能对 wxPython 本身说 mych,但我猜测它在显示之前尝试将文本转换为 Unicode,如果你有一个像 '123\xB0' 这样的字符串并尝试转换使用默认编码 (ASCII) 将其转换为 Unicode,然后它将抛出 UnicodeDecodeError。 您可以通过替换来解决此问题

s = message.get_string()

s = message.get_string().decode('ISO8859-1')

其中我假设函数 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 throw UnicodeDecodeError. You can probably fix this by replacing

s = message.get_string()

with

s = message.get_string().decode('ISO8859-1')

(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.

清君侧 2024-07-15 20:59:26

当这个问题被问到时,情况可能有所不同,但我对任何偶然发现这个问题的人的想法是:

问题是 wxPython 正在尝试转换为 unicode,并且缺乏字符集信息,它尝试使用 ASCII,这是无效的。 如果您知道您的数据是 utf-8,请告诉它,它就会正常工作。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wx

app = wx.PySimpleApp()
app.TopWindow = wx.Frame(None)
field = wx.TextCtrl(app.TopWindow)

string_data = '°'
print type(string_data)
# this would error, as it tries to convert to unicode from ascii
# field.Value += string_data

unicode_data = unicode(string_data, 'utf-8')
print type(unicode_data)
field.Value += unicode_data
app.TopWindow.Show()
app.MainLoop()

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.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wx

app = wx.PySimpleApp()
app.TopWindow = wx.Frame(None)
field = wx.TextCtrl(app.TopWindow)

string_data = '°'
print type(string_data)
# this would error, as it tries to convert to unicode from ascii
# field.Value += string_data

unicode_data = unicode(string_data, 'utf-8')
print type(unicode_data)
field.Value += unicode_data
app.TopWindow.Show()
app.MainLoop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文