2

I have a dictionary of currencies here in JSON.

I will give a sample for simplicity:

{
    "AED": "united Arab Emirates Dirham",
    "AFN": "Afghan Afghani",
    ...
    "ZWL": "Zimbabwean Dollar"
}

And I wish to add them to a file where i can continuously add different sets of currencies at different times. The file should have a column for the code name of currency (e.g. "AED") and another column for name.

I really don't know where to start. Help to point me in the right direction will be very much appreciated.

My code for the dictionary is as follows:

import json
import urllib.request
def _fetch_currencies():
    f = urllib.request.urlopen(
        'http://openexchangerates.org/api/currencies.json')
    charset = f.info().get_param('charset', 'utf8')
    data = f.read()
    decoded = json.loads(data.decode(charset))
    print(json.dumps(decoded, indent=4))
Francis Colas
  • 3,459
  • 2
  • 26
  • 31
PeteG
  • 49
  • 7
  • 1
    There are multiple ways to do this. 1. Use csv as suggested in the answer 2. write json data to file ref. http://stackoverflow.com/questions/12309269/write-json-data-to-file-in-python. 3. Write a configParser file using configPraser https://docs.python.org/2/library/configparser.html – cmidi Apr 06 '15 at 14:43

2 Answers2

5

You could simply save your data in csv with one line per currency:

AED, united Arab Emirates Dirham
AFN, Afghan Afghani
ZWL, Zimbabwean Dollar

To do that, you might want to transform your dictionary into rows, but in this case, it's trivial, since it's just the pair (key, value):

rows = decoded.items()

Note, however, that the items will be in a random order, if you want the entries sorted, you can sort them before writing to the file:

rows.sort()

In the end, using the csv module:

import csv
with open('local_file.csv', 'w') as my_csv:
    csv_writer = csv.writer(my_csv, delimiter=',')
    csv_writer.writerows(sorted(decoded.items()))

Putting it all together:

import json
import urllib.request
import csv

def fetch_currencies():
    f = urllib.request.urlopen('http://openexchangerates.org/api/currencies.json')
    charset = f.info().get_param('charset', 'utf8')
    data = f.read()
    decoded = json.loads(data.decode(charset))
    return decoded

def save_currencies(currencies, filename):
    sorted_currencies = sorted(currencies.items())
    with open(filename, 'w') as my_csv:
        csv_writer = csv.writer(my_csv, delimiter=',')
        csv_writer.wrtiterows(sorted_currencies)

save_currencies(fetch_currencies(), 'currencies.csv')
Francis Colas
  • 3,459
  • 2
  • 26
  • 31
1

You can use csv.DictWriter to handle easily the save file process. As DictWriter handles with dictionaries and the result of json.loads is dict, DictWriter do the job simpler.

import csv
import json
import urllib.request

def _fetch_currencies():
    f = urllib.request.urlopen('http://openexchangerates.org/api/currencies.json')
    charset = f.info().get_param('charset', 'utf8')
    data = f.read()
    decoded = json.loads(data.decode(charset))
    with open('names.csv', 'w') as csvfile:
        fieldnames = ['code', 'country']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        writer.writeheader()
        for code, country in decoded.items():
            writer.writerow({'code': code, 'country': country})
Mauro Baraldi
  • 6,346
  • 2
  • 32
  • 43
  • File "C:\Users\daniel\Documents\Python Coursework\_fetch_currencies().py", line 16, in _fetch_currencies writer.writerow({'code': code, 'country': country}) File "C:\Users\daniel\Anaconda3\lib\csv.py", line 153, in writerow return self.writer.writerow(self._dict_to_list(rowdict)) File "C:\Users\daniel\Anaconda3\lib\encodings\cp1252.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\u02bb' in position 13: character maps to – PeteG Apr 06 '15 at 14:51
  • There is nothing wrong with the answer. Take a look [here](http://stackoverflow.com/questions/25127935/unicodeencodeerror-charmap-codec-cant-encode-character-problems) – Mauro Baraldi Apr 06 '15 at 14:56