Python论坛  - 讨论区

标题:[python-chinese] 同一个程序在命令行环境下和pythonwin执行的结果不一样

2006年01月26日 星期四 00:26

Liu Jun gz19990909 at 163.com
Thu Jan 26 00:26:28 HKT 2006

我写了一个解码sms PDU的程序如下:
def to_chinese(userdata):
    '''
    Translate the userdata string  into 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



[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年01月26日 星期四 00:49

Andelf andelf at gmail.com
Thu Jan 26 00:49:15 HKT 2006

字符串转换编码时,如果有一个字符不能被转换,那么就会引起一个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 string  into 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

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号