Unicode API响应抛出错误'' ascii'编解码器不能编码字符u' \ u2019'位置22462'

时间:2016-06-15 23:17:49

标签: python unicode utf-8

我正在进行API调用,响应中包含unicode字符。将此响应加载到文件中会引发以下错误:

nil

我已尝试过所有解码和编码组合(' utf-8')。

以下是代码:

'ascii' codec can't encode character u'\u2019' in position 22462

许多开发人员都面临这个问题。很多地方都要求使用url = "https://%s?start_time=%s&include=metric_sets,users,organizations,groups" % (api_path, start_epoch) while url != None and url != "null" : json_filename = "%s/%s.json" % (inbound_folder, start_epoch) try: resp = requests.get(url, auth=(api_user, api_pwd), headers={'Content-Type': 'application/json'}) except requests.exceptions.RequestException as e: print "|********************************************************|" print e return "Error: {}".format(e) print "|********************************************************|" sys.exit(1) try: total_records_extracted = total_records_extracted + rec_cnt jsonfh = open(json_filename, 'w') inter = resp.text string_e = inter#.decode('utf-8') final = string_e.replace('\\n', ' ').replace('\\t', ' ').replace('\\r', ' ')#.replace('\\ ',' ') encoded_data = final.encode('utf-8') cleaned_data = json.loads(encoded_data) json.dump(cleaned_data, jsonfh, indent=None) jsonfh.close() except ValueError as e: tb = traceback.format_exc() print tb print "|********************************************************|" print e print "|********************************************************|" sys.exit(1) 或在python顶部使用.decode('utf-8')

它仍然没有帮助。

有人可以帮我解决这个问题吗?

这是跟踪:

# _*_ coding:utf-8 _*_

1 个答案:

答案 0 :(得分:1)

inter = resp.text
string_e = inter#.decode('utf-8')
encoded_data = final.encode('utf-8')

text属性是一个Unicode字符串,使用从HTTP头中使用的请求模块可能正在使用的任何编码从原始字节解码。

你可能不希望这样; JSON对编码应该是什么有自己的想法,所以你应该让JSON解码器通过从resp.content获取原始响应字节并将它们直接传递给json.loads来实现。

此外,请求还有一种快捷方法:resp.json()

final = string_e.replace('\\n', ' ').replace('\\t', ' ').replace('\\r', ' ')#.replace('\\ ',' ')

尝试在JSON-string-literal格式化输入上执行此操作是一个坏主意:您将错过一些有效的转义,并错误地忽略其他转义。你的实际错误与Unicode完全没有关系,而是这个替换正在破坏输入。例如,考虑输入JSON:

{"message": "Open the file C:\\newfolder\\text.txt"}

更换后:

{"message": "Open the file C:\ ewfolder\ ext.txt"}

显然无效JSON。

您应该让json解码输入,然后过滤结构化输出中的所有字符串,而不是尝试对JSON编码的字符串进行操作。这可能涉及使用递归函数向下走到数据的每个级别,寻找要过滤的字符串。例如

def clean(data):
    if isinstance(data, basestring):
        return data.replace('\n', ' ').replace('\t', ' ').replace('\r', ' ')
    if isinstance(data, list):
        return [clean(item) for item in data]
    if isinstance(data, dict):
        return {clean(key): clean(value) for (key, value) in data.items()}
    return data

cleaned_data = clean(resp.json())