简体   繁体   English

请求-Python解析JSON错误-使用编解码器加载

[英]Requests - Python Parsing JSON error - load with codecs

I am trying to parse data from an api with python and requests. 我正在尝试使用python和请求从api解析数据。

SO Reference Python codecs and utf-8 bom error SO参考Python编解码器utf-8 bom错误

Listed multiple references above as I have updated script with each error received. 上面列出了多个参考,因为我已经更新了脚本并收到了每个错误。

import requests
import codecs
import json

r = requests.get(
    "https://api.tatts.com/sales/vmax/web/data/racing/2017/4/05/mr/")
data = json.load(codecs.open(r.json(), 'utf-8-sig'))
# reads = r.json()
# data = reads.decode('utf-8-sig')

with open('data.json', 'w') as f:
    json.dump(data, f)

I want to save the response from the api https://api.tatts.com/sales/vmax/web/data/racing/2017/4/05/mr/ to a file.json 我想将响应从api https://api.tatts.com/sales/vmax/web/data/racing/2017/4/05/mr/保存到file.json

Initially I received the below so applied codecs resolution from SO reference answer. 最初,我从SO参考答案中收到了以下适用的编解码器解析。

json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig): line 1 column 1 (char 0)

this resolution from SO answer. 从SO答案这个决议。

data = json.load(codecs.open(r.json(), 'utf-8-sig'))

Now I receive error that 现在我收到错误消息

TypeError: expected str, bytes or os.PathLike object, not dict

However I cannot resolve the typerror because I need to load using codecs to stop the ut8-sig error. 但是,我无法解决typerror,因为我需要使用编解码器进行加载以停止ut8​​-sig错误。

How can I parse and write from requests and avoid both errors? 如何解析请求并从请求中写入内容,同时避免这两个错误?

EDIT 编辑

Updated using below answer, however fails to write the file to disk. 使用以下答案进行了更新,但是无法将文件写入磁盘。

import requests
import codecs
import json

r = requests.get(
    "https://api.tatts.com/sales/vmax/web/data/racing/2017/4/05/mr/")
data = json.load(codecs.open(r.text, 'r', 'utf-8-sig'))

with open('data.json', 'w') as f:
    f.write(data)

Answer 回答

import requests
import json

r = requests.get(
    "https://api.tatts.com/sales/vmax/web/data/racing/2017/4/05/mr/")

output = open('data.json', 'w')
output.write(r.text)

codecs.open opens a local file using a given encoding. codecs.open使用给定的编码打开本地文件。 codecs.decode will convert an in-memory object. codecs.decode将转换内存中的对象。 So I think you're after: 所以我认为您正在追求:

data = json.load(codecs.decode(r.text, 'utf-8-sig'))

Note that I've used r.text which means the requests library will not attempt to do any parsing of its own. 请注意,我使用了r.text ,这意味着请求库将不会尝试自行进行任何解析。 Unless you want to modify the data before saving though, you could just save the response directly to disk: 除非您要在保存之前修改数据,否则您可以将响应直接保存到磁盘上:

with open('data.json', 'w') as f:
    f.write(r.text)

Answer your updated question. 回答您的最新问题。 You did not reach the code of writing data to file, If you scroll up your output I believe the error you got is: 您没有达到将数据写入文件的代码,如果您向上滚动输出,我相信您得到的错误是:

IOError: [Errno 63] File name too long:... IOError:[Errno 63]文件名太长:...

The first parameter of codecs.open(r.text, 'r', 'utf-8-sig') is filename , as you can find out following docs of codecs.open . codecs.open(r.text, 'r', 'utf-8-sig')的第一个参数是filename ,您可以找到以下codecs.open文档 I think Alex Taylor's answer is enough to write response to a file, but if you really need to decode the response, you could try: 我认为Alex Taylor的答案足以将响应写入文件,但是如果您确实需要解码响应,则可以尝试:

data = codecs.decode(str(response.text), 'utf-8-sig')

Another error in your code: data = json.load(codecs.open(r.text, 'r', 'utf-8-sig')) make data to be type of unicode , you can't write an unicode object to file. 代码中的另一个错误: data = json.load(codecs.open(r.text, 'r', 'utf-8-sig'))使data成为unicode类型,您无法将unicode对象写入文件。 you can just dump it to your file: 您可以将其转储到文件中:

import requests
import json
import codecs

r = requests.get("https://api.tatts.com/sales/vmax/web/data/racing/2017/4/05/mr/")
data = codecs.decode(str(r.text), 'utf-8-sig')

with open('data.json', 'w') as f:
    json.dump(data, f)

And you can load it back later with code: 您可以稍后使用代码将其加载回:

with open('data.json', 'r') as f:
    data = json.load(f)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM