Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 编解码器可以';t在236-237位置对字符进行编码_Python_Json_Api_Request - Fatal编程技术网

Python 编解码器可以';t在236-237位置对字符进行编码

Python 编解码器可以';t在236-237位置对字符进行编码,python,json,api,request,Python,Json,Api,Request,我正在尝试从中获取json文件。 这是我的代码: import requests import json headers = { 'Accept': 'application/json', } response = {} data = {} response = requests.get('http://acnhapi.com/v1/villagers/1', headers=headers) profile = response.text open('profile.json',

我正在尝试从中获取json文件。 这是我的代码:


import requests
import json

headers = {
    'Accept': 'application/json',
}
response = {}
data = {}
response = requests.get('http://acnhapi.com/v1/villagers/1', headers=headers)
profile = response.text
open('profile.json', 'w').write(profile)
with open('profile.json') as json_file:
    data = json.load(json_file)

print(data)
当我运行它时,会出现以下错误:

Traceback (most recent call last):
  File "villager.py", line 11, in <module>
    open('profile.json', 'w').write(profile)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2032.0_x64__qbz5n2kfra8p0\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 236-237: character maps to <undefined>
回溯(最近一次呼叫最后一次):
文件“villager.py”,第11行,在
打开('profile.json','w')。写入(profile)
文件“C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7.2032.0_x64_uuQBZ5N2KFRA8P0\lib\encodings\cp1252.py”,第19行,编码
返回codecs.charmap\u encode(输入、自身错误、编码表)[0]
UnicodeEncodeError:“charmap”编解码器无法对位置236-237中的字符进行编码:字符映射到

我不明白为什么会有这个错误。你有什么想法吗?

由于UTF-8字符集,这是失败的。您可以使用
编解码器
模块解决此问题

import requests
import json
import codecs
headers = {
    'Accept': 'application/json',
}



response = {}
data = {}
response = requests.get('http://acnhapi.com/v1/villagers/1', headers=headers)
profile = response.text
file = codecs.open('profile.json', "w", "utf-8")
file.write(profile)
file.close()
with codecs.open('profile.json', "r", "utf-8") as json_file:
    data = json.load(json_file)

print(data)