简体   繁体   中英

How to save each loop to a new file with python

I trying to get a json response and save it as a new file. To do that am I trying to loop a request through a range, then I want to save each json result as a new file

I managed to pull the request through the loop but I don't know how to save it each output in new file.

import requests
import json

for i in range(1, 5):
    vino = requests.get('https://www.vivino.com/api/explore/explore?country_code=dk&currency_code=DKK&grape_filter=varietal&merchant_id=&min_rating=1&order_by=&order=desc&page='+str(i)+'&price_range_max=2500&price_range_min=0&wine_type_ids[]=1&wine_type_ids[]=2')
    data = vino.json()
    with open('data' + str(i) + '.json', 'w') as outfile:
    json.dump(data, outfile)

not sure why Milad suggested using a write() — your code is basically fine, it just needs the indentation being done correctly. you can either do:

import requests
import json

for i in range(1, 5):
    vino = requests.get('https://www.vivino.com/api/explore/explore?country_code=dk&currency_code=DKK&grape_filter=varietal&merchant_id=&min_rating=1&order_by=&order=desc&page='+str(i)+'&price_range_max=2500&price_range_min=0&wine_type_ids[]=1&wine_type_ids[]=2')
    data = vino.json()
    with open('data' + str(i) + '.json', 'w') as outfile:
       json.dump(data, outfile)

note the extra spaces at the beginning of the last line, they are important and is what people are referring to with indentation. reading some Python tutorials might help

making this code slightly nicer, you could use more of the requests interface by doing:

import requests
import json

for page in range(1, 5):
    vino = requests.get('https://www.vivino.com/api/explore/explore', params={
        'country_code': 'dk',
        'currency_code': 'DKK',
        'grape_filter': 'varietal',
        'min_rating': 1,
        'page': page,
        'wine_type_ids[]': [1, 2],
    })

    # raise an exception if this request wasn't successful
    vino.raise_for_status()

    data = vino.json()
    with open('data' + str(page) + '.json', 'w') as outfile:
       json.dump(data, outfile)

if you're going to be reading the output yourself I'd "pretty print" the output with something like:

    with open('data' + str(page) + '.json', 'w') as outfile:
       json.dump(data, outfile, sort_keys=True,
                 indent=4, separators=(',', ': '))

finally, if you're using a recent version of Python3, I'd also use the the new "format strings":

    with open(f'data{page}.json', 'w') as outfile:
       json.dump(data, outfile, sort_keys=True,
                 indent=4, separators=(',', ': '))

as it tends to result in easier to read code…

do it this way:

import requests
import json

for i in range(1, 5):
    vino = requests.get('https://www.vivino.com/api/explore/explore?country_code=dk&currency_code=DKK&grape_filter=varietal&merchant_id=&min_rating=1&order_by=&order=desc&page='+str(i)+'&price_range_max=2500&price_range_min=0&wine_type_ids[]=1&wine_type_ids[]=2')
    data = vino.json()
    with open('data' + str(i) + '.json', 'w') as outfile:
        outfile.write(data)

The response from the website already contains json data, but as a string. If you want to save it to the file, you can just save that string, no need to call the json() function on the response object. json() method will parse this data to python objects (dicts and lists). You can use that after to fiddle with the data. If you just want to save it, this is not needed.

This will work:

import requests

for i in range(1, 5):
    resp = requests.get('https://www.vivino.com/api/explore/explore?country_code=dk&currency_code=DKK&grape_filter=varietal&merchant_id=&min_rating=1&order_by=&order=desc&page='+str(i)+'&price_range_max=2500&price_range_min=0&wine_type_ids[]=1&wine_type_ids[]=2')
    data = resp.text

    with open("data-{}.json".format(i), 'w') as outfile:
        outfile.write(data)

Try this

import requests
import json

for i in range(1, 5):
    vino = requests.get('https://www.vivino.com/api/explore/explore?country_code=dk&currency_code=DKK&grape_filter=varietal&merchant_id=&min_rating=1&order_by=&order=desc&page='+str(i)+'&price_range_max=2500&price_range_min=0&wine_type_ids[]=1&wine_type_ids[]=2')
    data = vino.json()
    with open('data' + str(i) + '.json', 'w') as outfile:
        json.dump(data, outfile)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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