2006年01月26日 星期四 00:26
我写了一个解码sms PDU的程序如下: def to_chinese(userdata): ''' Translate the userdata stringinto Chinese and save it in a tuple list ''' result=[] ud=split_userdata(userdata) #split the userdata into 2byte in a word for word in ud: #for each 2Bytes word in userdata list result.append(unicode(unichr(int(word,16)))) #to Chinese return result def print_pdu(seq,pdu): print '\r' print ' No.: %s\r' % seq print ' HEAD: %s\r' % pdu['HEAD'] print ' TYPE: %s\r' % pdu['TYPE'] print ' TIME: %s\r' % get_time(pdu['TIME']) print 'TEL NUM: %s\r' % get_isdn(pdu['ADD-NUM']) s=''.join(to_chinese(pdu['UD'])) print 'CONTENT: %s\r' % s 在解一个特殊的PDU时,在pythonwin(python2.4.1)下执行时没有遇到错误,但 输出了“??”字符,然后继续执行后面的语句。但在 windows命令行下执行却发生 以下错误,并停了下来。这是为什么? Traceback (most recent call last): File "readsms.py", line 203, in ? main() File "readsms.py", line 200, in main print_pdu(c+1,a) File "readsms.py", line 147, in print_pdu print 'CONTENT: %s\r' % s UnicodeEncodeError: 'gbk' codec can't encode character u'\u0500' in position 9: illegal multibyte sequence
2006年01月26日 星期四 00:49
字符串转换编码时,如果有一个字符不能被转换,那么就会引起一个UnicodeError异常.比如,如果编码为'ascii', Unicode字符U+1F28 就不能被转换,因为它的值太大. 同样地,字符串 "\xfc" 也不能被转换到Unicode,因为它包含一个在ASCII字符之外的字符 pythinwin 在转换编码的时候 unicode(s [, encoding [,errors ]]) 中 errors参数应该为replace 而windows命令行下为默认 , strict 2006/1/26, Liu Jun <gz19990909 at 163.com>: > > > 我写了一个解码sms PDU的程序如下: > def to_chinese(userdata): > ''' > Translate the userdata stringinto Chinese > and save it in a tuple list > ''' > result=[] > ud=split_userdata(userdata) #split the userdata into 2byte in a word > > for word in ud: #for each 2Bytes word in userdata list > result.append(unicode(unichr(int(word,16)))) #to Chinese > > return result > > def print_pdu(seq,pdu): > print '\r' > print ' No.: %s\r' % seq > print ' HEAD: %s\r' % pdu['HEAD'] > print ' TYPE: %s\r' % pdu['TYPE'] > print ' TIME: %s\r' % get_time(pdu['TIME']) > print 'TEL NUM: %s\r' % get_isdn(pdu['ADD-NUM']) > > s=''.join(to_chinese(pdu['UD'])) > print 'CONTENT: %s\r' % s > > 在解一个特殊的PDU时,在pythonwin(python2.4.1)下执行时没有遇到错误,但 > 输出了"??"字符,然后继续执行后面的语句。但在 windows命令行下执行却发生 > 以下错误,并停了下来。这是为什么? > > Traceback (most recent call last): > File "readsms.py", line 203, in ? > main() > File "readsms.py", line 200, in main > print_pdu(c+1,a) > File "readsms.py", line 147, in print_pdu > print 'CONTENT: %s\r' % s > UnicodeEncodeError: 'gbk' codec can't encode character u'\u0500' in > position 9: > illegal multibyte sequence > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > -- Andelf BLOG:http://blog.sohu.com/members/andelf/ BLOG:http://spaces.msn.com/members/andelf -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060126/12313708/attachment.html
Zeuux © 2025
京ICP备05028076号