Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Thu Jun 29 11:26:56 2017
  5.  
  6. @author: saliksyed
  7. """
  8. import csv
  9.  
  10. gdp_data = csv.reader(open("gdp.dat"))
  11.  
  12. gdp_by_country_code = {}
  13. for row in gdp_data:
  14. country = row[0]
  15. country_code = row[1]
  16. gdp_values = []
  17. for value in row[5:]:
  18. # We need to do this because there is some missing data which is represented by ""
  19. if value == "":
  20. final_parsed_value = None
  21. else:
  22. final_parsed_value = float(value)
  23.  
  24. gdp_values.append(final_parsed_value)
  25.  
  26. country_data = {}
  27.  
  28. country_data["country"] = country
  29. country_data["country_code"] = country_code
  30. country_data["gdp_values"] = gdp_values
  31. gdp_by_country_code[country_code] = country_data
  32.  
  33.  
  34.  
  35. population_data = csv.reader(open("population.dat","r"))
  36.  
  37. population_by_country_code = {}
  38. for row in population_data:
  39. country = row[0]
  40. country_code = row[1]
  41. population_data = []
  42. for value in row[5:]:
  43. if value == "":
  44. population_data.append(None)
  45. else:
  46. population_data.append(float(value))
  47. country_data = {}
  48. country_data["country"] = country
  49. country_data["country_code"] = country_code
  50. country_data["population_values"] = population_data
  51. population_by_country_code[country_code] = country_data
  52.  
  53.  
  54.  
  55. per_capita_gdp_by_country_code = {}
  56. for country_code in population_by_country_code.keys():
  57. if country_code in population_by_country_code and country_code in gdp_by_country_code:
  58. gdp = gdp_by_country_code[country_code]["gdp_values"]
  59. population = population_by_country_code[country_code]["population_values"]
  60. name = gdp_by_country_code[country_code]["country"]
  61. gdp_per_capita = []
  62. for i in xrange(0, len(gdp)):
  63. if gdp[i] and population[i]:
  64. gdp_per_capita.append(gdp[i]/population[i])
  65. else:
  66. gdp_per_capita.append(None)
  67. country_data = {}
  68. country_data["country"] = country
  69. country_data["country_code"] = country_code
  70. country_data["gdp_per_capita"] = gdp_per_capita
  71. per_capita_gdp_by_country_code[country_code] = country_data
  72.  
  73. print per_capita_gdp_by_country_code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement