1

I need help changing the name of the .txt output file to the date inside the json file, is it possible?

Json:

"date": "2018-03-21",

python:

import urllib.request, json

with urllib.request.urlopen("http://api.fixer.io/latest?&base=EUR") as url:
    data=json.loads(url.read().decode())
    print(data)

    with open('*changeNameHere*{}.txt'.format(*Example*), 'w') as outfile:
        json.dump(data["rates"], outfile)
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Filipe Santos
  • 371
  • 1
  • 2
  • 11

1 Answers1

1

Simply do:

import urllib.request, json

with urllib.request.urlopen("http://api.fixer.io/latest?&base=EUR") as url:
    data=json.loads(url.read().decode())
    print(data)

    with open('{}.txt'.format(data['date']), 'w') as outfile:
        json.dump(data["rates"], outfile)
drec4s
  • 7,946
  • 8
  • 33
  • 54