How to save each loop to a new file with python





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















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)









share|improve this question




















  • 2





    Shouldn't there be an indentation at json.dump ?

    – Melon
    Jan 3 at 12:10











  • Im new to it all. Sorry but I don't know what you mean by "indentation"

    – those44244
    Jan 3 at 12:13











  • don't even start the war: spaces or tabs?

    – naivepredictor
    Jan 3 at 12:14











  • trying to use tabs

    – those44244
    Jan 3 at 12:24











  • I get it, thanks and it worked!

    – those44244
    Jan 3 at 13:18


















0















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)









share|improve this question




















  • 2





    Shouldn't there be an indentation at json.dump ?

    – Melon
    Jan 3 at 12:10











  • Im new to it all. Sorry but I don't know what you mean by "indentation"

    – those44244
    Jan 3 at 12:13











  • don't even start the war: spaces or tabs?

    – naivepredictor
    Jan 3 at 12:14











  • trying to use tabs

    – those44244
    Jan 3 at 12:24











  • I get it, thanks and it worked!

    – those44244
    Jan 3 at 13:18














0












0








0


1






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)









share|improve this question
















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)






python range output






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 12:08









Rakesh

41.6k134276




41.6k134276










asked Jan 3 at 12:07









those44244those44244

125




125








  • 2





    Shouldn't there be an indentation at json.dump ?

    – Melon
    Jan 3 at 12:10











  • Im new to it all. Sorry but I don't know what you mean by "indentation"

    – those44244
    Jan 3 at 12:13











  • don't even start the war: spaces or tabs?

    – naivepredictor
    Jan 3 at 12:14











  • trying to use tabs

    – those44244
    Jan 3 at 12:24











  • I get it, thanks and it worked!

    – those44244
    Jan 3 at 13:18














  • 2





    Shouldn't there be an indentation at json.dump ?

    – Melon
    Jan 3 at 12:10











  • Im new to it all. Sorry but I don't know what you mean by "indentation"

    – those44244
    Jan 3 at 12:13











  • don't even start the war: spaces or tabs?

    – naivepredictor
    Jan 3 at 12:14











  • trying to use tabs

    – those44244
    Jan 3 at 12:24











  • I get it, thanks and it worked!

    – those44244
    Jan 3 at 13:18








2




2





Shouldn't there be an indentation at json.dump ?

– Melon
Jan 3 at 12:10





Shouldn't there be an indentation at json.dump ?

– Melon
Jan 3 at 12:10













Im new to it all. Sorry but I don't know what you mean by "indentation"

– those44244
Jan 3 at 12:13





Im new to it all. Sorry but I don't know what you mean by "indentation"

– those44244
Jan 3 at 12:13













don't even start the war: spaces or tabs?

– naivepredictor
Jan 3 at 12:14





don't even start the war: spaces or tabs?

– naivepredictor
Jan 3 at 12:14













trying to use tabs

– those44244
Jan 3 at 12:24





trying to use tabs

– those44244
Jan 3 at 12:24













I get it, thanks and it worked!

– those44244
Jan 3 at 13:18





I get it, thanks and it worked!

– those44244
Jan 3 at 13:18












4 Answers
4






active

oldest

votes


















1














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…






share|improve this answer
























  • Thank you so much. I understod how the indentation caused the problem. I'm on my 20th hour with python and first time coding. Thanks a lot Sam!

    – those44244
    Jan 3 at 13:28



















0














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)





share|improve this answer
























  • Im getting outfile.write(data) TypeError: expected a character buffer object

    – those44244
    Jan 3 at 12:24



















0














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)





share|improve this answer































    0














    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)





    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54021986%2fhow-to-save-each-loop-to-a-new-file-with-python%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      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…






      share|improve this answer
























      • Thank you so much. I understod how the indentation caused the problem. I'm on my 20th hour with python and first time coding. Thanks a lot Sam!

        – those44244
        Jan 3 at 13:28
















      1














      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…






      share|improve this answer
























      • Thank you so much. I understod how the indentation caused the problem. I'm on my 20th hour with python and first time coding. Thanks a lot Sam!

        – those44244
        Jan 3 at 13:28














      1












      1








      1







      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…






      share|improve this answer













      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…







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jan 3 at 12:53









      Sam MasonSam Mason

      3,36811331




      3,36811331













      • Thank you so much. I understod how the indentation caused the problem. I'm on my 20th hour with python and first time coding. Thanks a lot Sam!

        – those44244
        Jan 3 at 13:28



















      • Thank you so much. I understod how the indentation caused the problem. I'm on my 20th hour with python and first time coding. Thanks a lot Sam!

        – those44244
        Jan 3 at 13:28

















      Thank you so much. I understod how the indentation caused the problem. I'm on my 20th hour with python and first time coding. Thanks a lot Sam!

      – those44244
      Jan 3 at 13:28





      Thank you so much. I understod how the indentation caused the problem. I'm on my 20th hour with python and first time coding. Thanks a lot Sam!

      – those44244
      Jan 3 at 13:28













      0














      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)





      share|improve this answer
























      • Im getting outfile.write(data) TypeError: expected a character buffer object

        – those44244
        Jan 3 at 12:24
















      0














      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)





      share|improve this answer
























      • Im getting outfile.write(data) TypeError: expected a character buffer object

        – those44244
        Jan 3 at 12:24














      0












      0








      0







      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)





      share|improve this answer













      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)






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jan 3 at 12:15









      Milad KhMilad Kh

      347313




      347313













      • Im getting outfile.write(data) TypeError: expected a character buffer object

        – those44244
        Jan 3 at 12:24



















      • Im getting outfile.write(data) TypeError: expected a character buffer object

        – those44244
        Jan 3 at 12:24

















      Im getting outfile.write(data) TypeError: expected a character buffer object

      – those44244
      Jan 3 at 12:24





      Im getting outfile.write(data) TypeError: expected a character buffer object

      – those44244
      Jan 3 at 12:24











      0














      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)





      share|improve this answer




























        0














        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)





        share|improve this answer


























          0












          0








          0







          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)





          share|improve this answer













          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)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 12:59









          iborkoiborko

          514




          514























              0














              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)





              share|improve this answer




























                0














                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)





                share|improve this answer


























                  0












                  0








                  0







                  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)





                  share|improve this answer













                  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)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 13:03









                  Allef MizaelAllef Mizael

                  112




                  112






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54021986%2fhow-to-save-each-loop-to-a-new-file-with-python%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                      How to fix TextFormField cause rebuild widget in Flutter