i trying parse data api python , requests.
so reference python codecs , utf-8 bom error
listed multiple references above have updated script 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') open('data.json', 'w') f: json.dump(data, f)
i want save response api https://api.tatts.com/sales/vmax/web/data/racing/2017/4/05/mr/ file.json
initially received below applied codecs resolution reference answer.
json.decoder.jsondecodeerror: unexpected utf-8 bom (decode using utf-8-sig): line 1 column 1 (char 0)
this resolution answer.
data = json.load(codecs.open(r.json(), 'utf-8-sig'))
now receive error that
typeerror: expected str, bytes or os.pathlike object, not dict
however cannot resolve typerror because need load using codecs stop ut8-sig error.
how can parse , write requests , avoid both errors?
edit
updated using below answer, fails write file 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')) open('data.json', 'w') 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 local file using given encoding. codecs.decode
convert in-memory object. think you're after:
data = json.load(codecs.decode(r.text, 'utf-8-sig'))
note i've used r.text
means requests library not attempt parsing of own. unless want modify data before saving though, save response directly disk:
with open('data.json', 'w') f: f.write(r.text)
Comments
Post a Comment