将Twilio sms正文转换为@符号的字符串编码错误 [英] converting Twilio sms body to a string - encoding error for @ symbol
问题描述
我使用Twilio通过短信向我的python webapp发送电子邮件地址。但是,当我试图转换包含电子邮件地址的短信主体时,我收到了一个异常。
I'm sending an email address to my python webapp by sms using Twilio. However, when I attempt to convert the sms body containing the email address I get an exception.
<type 'exceptions.UnicodeEncodeError'>, UnicodeEncodeError('ascii', u'myemail\xa1gmail.com', 13, 14, 'ordinal not in range(128)'), <traceback object at 0x10cca5f8>)
Traceback (most recent call last):
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~tester-sandbox/1.360113171877023968/website/sms.py", line 22, in post
from_body = str(cgi.escape(self.request.get('Body', None))).strip()
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa1' in position 13: ordinal not in range(128)
我知道这是一个编码错误,它在电子邮件地址中的@字符上磕磕绊绊。在google搜索这个问题后,我发现了一个建议:
I get that this is an encoding error that's stumbling on the @ character in the email address. And after googling this issue I found a recommendation of
sys.setdefaultencoding( "latin-1" )
但是,简单地用@符号代替@ ...。克服这个问题的最简单方法是什么?
But that simply replaced the @ with a � symbol.... What is the simplest way to overcome this?
推荐答案
我联系Twilio来了解它的底部,事实证明这不是一个python问题。当用户发送他们的电子邮件地址(通过短信)时,它会发送一个字符(而不是@),这不幸是非ascii - 因此是例外。
I contacted Twilio to get to the bottom of this and it turns out it's not a python problem. When a user sends in their email address (by sms) it sends in an ¡ character (instead of @) which is unfortunately non-ascii - hence the exception.
从Twilio:
From Twilio:
一些运营商尚未做出修正,允许在通过其网络发送的短信中保留正确的格式
,所以你会在一些运营商处理的短信中看到
的@符号腐败。
Some carriers have not made the fix to allow the proper formatting to be preserved in SMS messages sent over their networks, so you will see corruption of @ signs in SMS handled by some carriers.
一个潜在的问题是在你的
上启用unicode字符编码Twilio帐户。一个潜在的缺点是,你将被限制为发送70个字符或更少的
长度的短信
One potential fix is to enable unicode character encoding on your Twilio account. A potential downside to this is that you would be limited to sending SMS messages that are 70 characters or less in length
这篇关于将Twilio sms正文转换为@符号的字符串编码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!