我从jasmis-sms接收到一个编码字符串,原始字符串是áéíóú ñaña!
,jasmin进行了一些编码并给了我
����� �a�a!
(在我的终端中)
chardet将其检测为charset: windows-1252
但是,当尝试使用
Message.decode('windows-1252')
但这又回来了
<type 'exceptions.UnicodeEncodeError'>: 'ascii' codec can't encode character u'\xe1' in position 11: ordinal not in range(128)
字符串是来自于jasmin-sms的拦截Python脚本
该脚本由jasmin-sms通过smpp接收SMS时启动,它注入一个全局变量routable
,其中包含所有SMS的数据,完整代码为:
python2
import urllib
import urllib2
import re
import chardet
file=open('/opt/jasmin-scritps/interception/mo-interceptor.log','a')
file.write('===============================================================================\n')
file.write('Start logging...\n')
SMS_to = routable.pdu.params['destination_addr']
SMS_from = routable.pdu.params['source_addr']
SMS_message = routable.pdu.params['short_message']
file.write('To: [%s]\n' % SMS_to )
file.write('From: [%s]\n' % SMS_from )
file.write('ShortMessage: [%s]\n' % SMS_message.encode("hex") )
file.write('data-coding: [%s]\n' % routable.pdu.params['data_coding'] )
file.write('charset: %s\n' % chardet.detect( SMS_message )['encoding'] )
file.write('decoded: [%s]\n' % SMS_message )
file.write('SmppSatus: [%s]\n' % smpp_status )
file.write('Content: [%s]\n' % routable.pdu.params['short_message'] )
我不确定如何解决这个问题。
我们非常感谢您的帮助!