gpt4 book ai didi

python - (python/boto sqs) UnicodeDecodeError : 'ascii' codec can't decode byte 0xc3 in position 5: ordinal not in range(128)

转载 作者:太空宇宙 更新时间:2023-11-04 05:47:11 26 4
gpt4 key购买 nike

我无法使用 AWS SDK (boto) 在 python 中为 SQS 发送带有重音字符的消息。

版本

python :2.7.6博托:2.20.1

代码

   #!/usr/bin/env python
# -*- coding: utf-8 -*-

import boto.sqs
from boto.sqs.message import RawMessage

# print boto.Version
sqs_conn = boto.sqs.connect_to_region(
'my_region',
aws_access_key_id='my_kye',
aws_secret_access_key='my_secret_ky')
queue = sqs_conn.get_queue('my_queue')
queue.set_message_class(RawMessage)

msg = RawMessage()

body = '1 café, 2 cafés, 3 cafés ...'
msg.set_body(body)
queue.write(msg)

最佳答案

一个解决方案:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

完整代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import boto.sqs
from boto.sqs.message import RawMessage
import sys # <== added this line

reload(sys) # <== added this line
sys.setdefaultencoding('utf-8') # <== added this line

# print boto.Version
sqs_conn = boto.sqs.connect_to_region(
'my_region',
aws_access_key_id='my_kye',
aws_secret_access_key='my_secret_ky')
queue = sqs_conn.get_queue('my_queue')
queue.set_message_class(RawMessage)

msg = RawMessage()

body = '1 café, 2 cafés, 3 cafés ...'
msg.set_body(body)
queue.write(msg)

来源:https://pythonadventures.wordpress.com/2012/03/29/ascii-codec-cant-encode-character/#comment-4672

关于python - (python/boto sqs) UnicodeDecodeError : 'ascii' codec can't decode byte 0xc3 in position 5: ordinal not in range(128),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31721432/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com