【发布时间】:2018-04-25 08:56:15
【问题描述】:
我的 Python 脚本有问题。当我不使用以下字母时,一切正常:ą、ć、ź 等。使用 api 发送短信的脚本。在邮递员的作品中也是如此。蟒蛇 2.7。
我想编码为 UTF-8 但我有:{"timestamp":"2018-04-25T08:04:04.418+0000","status":400,"error":"Bad Request","message":"JSON parse error: Invalid UTF-8 start byte 0x9c\n
我的脚本:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import httplib
if len(sys.argv) < 3:
print("no agrs")
print("arg1: number")
print("arg2: sender")
print("arg3 and next: message")
else:
headers = {"Content-Type": "application/json; charset=utf8",
"Authorization": 'Basic xxxx'}
conn = httplib.HTTPConnection('xxxx', 5050)
message = ""
for i in sys.argv[3:]:
message = message + " " + i
conn.request("POST", "/my/url/",
"{\"receiver\": \"" + sys.argv[1] + "\",\"sender\": \"" + str(sys.argv[2]) + "\", \"content\": \"" + str(message) + "\"}",
headers)
response = conn.getresponse()
print(response.status, response.reason)
print(response.read())
if response.status == 200:
print("sent!")
conn.close()
我的错误:
{"timestamp":"2018-04-25T08:53:25.834+0000","status":400,"error":"Bad Request","message":"JSON parse error: Invalid UTF-8 start byte 0x9c\n at [Source: (PushbackInputStream); line: 1, column: 66]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid UTF-8 start byte 0x9c\n at [Source: (PushbackInputStream); line: 1, column: 66]\n at [Source: (PushbackInputStream); line: 1, column: 60] (through reference chain: my-method[\"content\"])","path":"path to my url"}
【问题讨论】:
-
您在代码中的什么地方使用了这些字符?
sys.argv[3:]? -
启动时的参数。我说你,脚本向我的号码发送消息很好,但问题在于字符:ąśćżźł 等。参数如下所示:sms.py 123456789 "sender" "message with characters like ąśćżźć"
-
查看我编辑的答案。
-
UnicodeDecodeError: 'utf8' codec can't decode byte 0x9c in position 5: invalid start byte -
您使用什么系统控制台?你的操作系统是什么?
标签: python json python-2.7 encoding