python - 上传错误:“latin-1”编解码器无法使用 JSON 数据上传对字符“\u2019”进行编码
问题描述
我正在使用 python 将一些 JSON 数据上传到应用程序 UI,但出现以下错误。
上传时出错:“latin-1”编解码器无法对位置 5735 中的字符“\u2019”进行编码:正文 ('â') 不是有效的 Latin-1。如果您想以 UTF-8 编码发送它,请使用 body.encode('utf-8')。
该程序从 sample.json 文件中获取输入,该文件包含一个特殊字符 ( ' ),这会给出错误。
价值:美国运通%?
我的代码如下所示:
def read_from_file(file_path, target_path=None):
try:
f = open(file_path, "r")
data = json.load(f)
f.close()
if target_path:
result_obj = []
for obj in data:
if target_path in obj['Key']:
result_obj.append(obj)
data = result_obj
except Exception as e:
print ("ERROR reading file:", e, file=sys.stderr)
exit(1)
return data
def upload(server, token, data):
params = {"token": token}
for obj in data:
try:
payload = obj['Value']
url = server + obj['Key']
response = requests.put(url, data=payload, params=params)
if response.status_code != 200:
raise Exception("HTTP code %s on PUT %s" % (response.status_code, url))
except Exception as e:
print ("ERROR uploading:", e, file=sys.stderr)
exit(1)
有人可以告诉我需要在哪里更改我的代码以在上传时包含特殊字符( ' )吗?
解决方案
推荐阅读
- amazon-web-services - 如何将云端 url 重定向到主域?
- javascript - TypeError:projects.map 不是函数(React)
- php - 为什么容器在中间件之前运行?
- flutter - 如何在 Flutter 上实现 BLoC 模式的 TDD
- c - 我必须为 _wdupenv_s 链接哪个库
- sql - SQL - 删除所有重复项并仅保留一个
- lisp - 根据条件从循环返回
- javascript - 迭代对象以创建一个新对象
- c - C 中的 LInkedList sizeof(Node) 出现奇怪错误
- python - mac 上 python 的 os.system('cls') 的替代方案