简体   繁体   English

python'ascii'编解码器无法编码字符

[英]python 'ascii' codec can't encode characters

I am very new to python. 我是python的新手。 I am trying to send messages to android CCS server using a python script. 我正在尝试使用python脚本向Android CCS服务器发送消息。 I get the messages from RabbitMQ and then send it to CCS through XMPP. 我从RabbitMQ获取消息,然后通过XMPP将其发送到CCS。 My code is based on the example code from android website. 我的代码基于android网站上的示例代码。 It looks like the below: 如下图所示:

client = xmpp.Client('gcm.googleapis.com', debug=['socket'])
client.connect(server=(SERVER,PORT), secure=1, use_srv=False)
auth = client.auth(USERNAME, PASSWORD)
if not auth:
  print 'Authentication failed!'
  sys.exit(1)

client.RegisterHandler('message', message_callback)

# RabbitMQ Start ....

connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='localhost'))
channel = connection.channel()

print ' [*] Waiting for messages. To exit press CTRL+C'

def callback(ch, method, properties, body):
    global client
    body = body.replace("\\","")
    try:
        send_queue.append(body)
        client.Process(1)
        flush_queued_messages()
    except Exception as ex:
        print ex


channel.basic_consume(callback,
                      queue=GCMQUEUE,
                      no_ack=True)

channel.start_consuming()

It works pretty good. 效果很好。 But my problem is when I have emojis in my queue I get it in hex format like 22\\xe2\\x91\\xa223 and it occurs the following error: 但是我的问题是,当队列中有表情符号时,会以十六进制格式(例如22 \\ xe2 \\ x91 \\ xa223)来获取表情符号,并且会发生以下错误:

python 'ascii' codec can't encode characters in position 366-367: ordinal not in range

I tried replacing the line : 我尝试替换该行:

send_queue.append(body)

To

 send_queue.append(body.encode('ascii','ignore'))

This way I can ignore the error, but this removes all the unicode characters. 这样,我可以忽略该错误,但这会删除所有的unicode字符。 How to fix this ? 如何解决呢?

You should ensure the input value of body to be string, if not use str(body) then encode it as mentioned in the comments, str(body).encode('utf-8') 您应该确保body的输入值为字符串,如果不使用str(body)则按照注释str(body).encode('utf-8')进行编码。

If it's a string already just do body.encode('utf-8') 如果已经是字符串,请执行body.encode('utf-8')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:'ascii'编解码器不能编码字符 - Python: 'ascii' codec can't encode characters Python UnicodeEncodeError:'ascii'编解码器无法编码字符 - Python UnicodeEncodeError: 'ascii' codec can't encode characters Scrapy:ASCII编解码器无法编码字符 - Scrapy: ascii' codec can't encode characters UnicodeEncodeError:'ascii'编解码器无法编码字符 - UnicodeEncodeError: 'ascii' codec can't encode characters python'ascii'编解码器无法编码字符 - python 'ascii' codec can't encode character Python:追溯 XML 文件错误? 和 ASCII 和 'ascii' 编解码器不能编码字符 - Python: Traceback XML-file error? and ASCII and 'ascii' codec can't encode characters UnicodeEncodeError:“ ascii”编解码器无法对不在范围内的字符进行编码(128) - UnicodeEncodeError: 'ascii' codec can't encode characters ordinal not in range(128) Python mmh3:UnicodeEncodeError:'ascii'编解码器无法在位置0-14处编码字符:序数不在范围内(128) - Python mmh3: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-14: ordinal not in range(128) Python:UnicodeEncodeError:'ascii'编解码器无法在位置34-39处编码字符:序数不在范围内(128) - Python: UnicodeEncodeError: 'ascii' codec can't encode characters in position 34-39: ordinal not in range(128) Python2.7 UnicodeEncodeError:'ascii'编解码器不能编码0-11位的字符:序号不在范围内(128) - Python2.7 UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-11: ordinal not in range(128)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM