所以,我正在尝试使用PyWinUSB,但是我不能走多远,因为我一直得到一个UnicodeEncodeError。
代码:
import pywinusb.hid as hid
hid.find_all_hid_devices()输出:
[Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xae' in position 91: ordinal not in range(128)请注意,这只发生在我的外部键盘和鼠标插入(这是微软无线组合)。
当我尝试相同的代码时,这就是Python3.4中的内容。
HID device (vID=0x045e, pID=0x00e3, v=0x0053); Microsft; Microsoft Wireless Optical Desktop\xae 2.20, Path: \\?\hid#vid_045e&pid_00e3&mi_01&col01#8&a279d5&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}
HID device (vID=0x045e, pID=0x00e3, v=0x0053); Microsft; Microsoft Wireless Optical Desktop\xae 2.20, Path: \\?\hid#vid_045e&pid_00e3&mi_01&col03#8&a279d5&0&0002#{4d1e55b2-f16f-11cf-88cb-001111000030}
HID device (vID=0x045e, pID=0x00e3, v=0x0053); Microsft; Microsoft Wireless Optical Desktop\xae 2.20, Path: \\?\hid#vid_045e&pid_00e3&mi_01&col04#8&a279d5&0&0003#{4d1e55b2-f16f-11cf-88cb-001111000030}但是,如果我尝试用Python3.4为每个项目做一个print,我会得到以下内容:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "C:\Python34\lib\encodings\cp437.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\xae' in position 91: character maps to <undefined>我有什么办法解决这个问题吗?
发布于 2015-11-14 20:13:59
这不是pyWinUsb问题,实际上是Python2.x上的设计问题。
默认情况下,在stdout写入时没有字符解码管理。
USB设备字符串是Unicode UTF-8编码的,因此安装字符解码器是安全的(来自pyWinUsb show_hids.py示例):
`if sys.version_info < (3,): import codecs output = codecs.getwriter('mbcs')(sys.stdout) else: # python3, you have to deal with encodings, try redirecting to any file output = sys.stdout`https://stackoverflow.com/questions/30277451
复制相似问题