PYTHON   8

get_countries_import.py

Guest on 3rd June 2021 05:40:36 AM

  1. from base import *
  2.  
  3. filepath_import = './data/countries_import.json'
  4. filepath_countries_topartists = './data/countries_topartists1000.json'
  5. filepath_origin = './data/artists_origin.csv'
  6.  
  7. countries = json.load(open(filepath_countries_topartists))
  8.  
  9. countries_no = len(countries)
  10. processed_no = 0
  11.  
  12. clean_json(filepath_import)
  13. imported = {}
  14.  
  15. origin = {}
  16. # origin data into json
  17. with io.open(filepath_origin, 'rb') as f:
  18.     reader = csv.reader(f, delimiter='\t', lineterminator='\n')
  19.     for row in reader:
  20.         if len(row) == 3:
  21.             origin[row[0]] = {
  22.                 "mbid": row[0],
  23.                 "name": row[1],
  24.                 "country": row[2]
  25.             }
  26.  
  27. for country in countries:
  28.     processed_no = processed_no + 1
  29.     imported[country['name']] = {}
  30.  
  31.     print ''
  32.     print 'processing country: ' + country['name'] + ', ' + str(
  33.         processed_no) + ' / ' + str(countries_no)
  34.  
  35.     for artist in country['artists']:
  36.         mbid = artist['mbid']
  37.         if mbid in origin:
  38.             try:
  39.                 origin_country = origin[artist['mbid']]['country']
  40.  
  41.                 if origin_country in imported[country['name']]:
  42.                     imported[country['name']][origin_country] += 1
  43.  
  44.                 else:
  45.                     imported[country['name']][origin_country] = 1
  46.  
  47.             except Exception as e:
  48.                 print('artist without origin ' + artist['name'] + ' not found')
  49.  
  50. store_json(filepath_import, imported, True)

Raw Paste


Login or Register to edit or fork this paste. It's free.