python – 无法转换为HEX – TypeError:找到非十六进制数字

行devicetoken = devicetoken.replace(”,”).decode(‘hex’)正在崩溃我的脚本.
这是设备令牌:(更改了一些数字)

devicetoken =’9cdcb815 d93e11ce 52baaf6c 14e27cc8 31d5ce62 2e51ce6d f75692c2 3617cadb’

一个推送通知发送正常,所以我确定设备令牌是好的,但在第一个事件后,我收到此错误

['"INBox" (UNSEEN 12)'] 
Sent Push alert.
Got an event!
['"INBox" (UNSEEN 13)']
Exception in thread Thread-4:Exception in thread Thread-4:
    Traceback (most recent call last):
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py",line 530,in __bootstrap_inner
        self.run()
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py",line 483,in run
        self.__target(*self.__args,**self.__kwargs)
      File "server.py",line 111,in idle
        self.dosync()
      File "server.py",line 118,in dosync
        sendPushNotification(numUnseen)
      File "server.py",line 54,in sendPushNotification
        devicetoken = devicetoken.replace(' ','').decode('hex')
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/hex_codec.py",line 42,in hex_decode
        output = binascii.a2b_hex(input)
    TypeError: Non-hexadecimal digit found

Link to the script

解决方法

您的设备令牌可能不是您认为的那样.也许它有换行或其他一些你看不到的角色.

作为一个简单的测试:

>>> devicetoken = '9cdcb815 d93e11ce 52baaf6c 14e27cc8 31d5ce62 2e51ce6d f75692c2 3617cadb'
>>> devicetoken.replace(' ','')
'9cdcb815d93e11ce52baaf6c14e27cc831d5ce622e51ce6df75692c23617cadb'
>>> devicetoken.replace(' ','').decode('hex')
'\x9c\xdc\xb8\x15\xd9>\x11\xceR\xba\xafl\x14\xe2|\xc81\xd5\xceb.Q\xcem\xf7V\x92\xc26\x17\xca\xdb'

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...