找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 1408|回复: 1

[求助] 新手求组。。想了很久都想不通。。

2

主题

3

帖子

3

积分

贫民

积分
3
weixiang1120 发表于 2019-11-17 23:28:17 | 显示全部楼层 |阅读模式
  1. [
  2.   {
  3.     "Country Name": "Arab World",
  4.     "Country Code": "ARB",
  5.     "Year": "2010",
  6.     "Value": "357868000"
  7.   },
  8.   {
  9.     "Country Name": "Caribbean small states",
  10.     "Country Code": "CSS",
  11.     "Year": "2010",
  12.     "Value": "6880000"
  13.   },
  14.   {
  15.     "Country Name": "East Asia & Pacific (all income levels)",
  16.     "Country Code": "EAS",
  17.     "Year": "2010",
  18.     "Value": "2201536674"
  19.   },
  20.   {
  21.     "Country Name": "East Asia & Pacific (developing only)",
  22.     "Country Code": "EAP",
  23.     "Year": "2010",
  24.     "Value": "1961558757"
  25.   },
  26.   {
  27.     "Country Name": "Euro area",
  28.     "Country Code": "EMU",
  29.     "Year": "2010",
  30.     "Value": "331766000"
  31.   },
  32.   {
  33.     "Country Name": "Europe & Central Asia (all income levels)",
  34.     "Country Code": "ECS",
  35.     "Year": "2010",
  36.     "Value": "890424544"
  37.   },
  38.   {
  39.     "Country Name": "Europe & Central Asia (developing only)",
  40.     "Country Code": "ECA",
  41.     "Year": "2010",
  42.     "Value": "405204000"
  43.   },
  44.   {
  45.     "Country Name": "European Union",
  46.     "Country Code": "EUU",
  47.     "Year": "2010",
  48.     "Value": "502125000"
  49.   },
  50.   {
  51.     "Country Name": "Heavily indebted poor countries (HIPC)",
  52.     "Country Code": "HPC",
  53.     "Year": "2010",
  54.     "Value": "635663000"
  55.   }
复制代码
上面这是population.py
  1. from pygal_maps_world.maps import COUNTRIES

  2. def get_country_code(country_name):
  3.     for code,name in COUNTRIES.items():
  4.         if name==country_name:
  5.             return code
  6.         else:
  7.             return None
复制代码
这是拿来查国家识别码的函数
  1. import pygal
  2. import json
  3. from country_code import get_country_code

  4. filename=r'C:\Users\Administrator\Downloads\Downloading-Data-master\population_data.json'
  5. with open(filename) as f:
  6.     f_data=json.load(f)

  7. for f_dict in f_data:
  8.     if f_dict['Year']=='2010':
  9.         country_name=f_dict['Country Name']
  10.         population=int(float(f_dict['Value']))
  11.         code=get_country_code(country_name)
  12.         if code:
  13.             print(code+':'+str(population))
  14.         else:
  15.             print('Error-'+country_name)
复制代码
这个代码主要就是查看国家识别码然后输入国家识别码 可是为什么有很多都是error的呢
  1. Error-Arab World
  2. Error-Caribbean small states
  3. Error-East Asia & Pacific (all income levels)
  4. Error-East Asia & Pacific (developing only)
  5. Error-Euro area
  6. Error-Europe & Central Asia (all income levels)
  7. Error-Europe & Central Asia (developing only)
  8. Error-European Union
  9. Error-Heavily indebted poor countries (HIPC)
  10. Error-High income
  11. Error-High income: nonOECD
  12. Error-High income: OECD
  13. Error-Latin America & Caribbean (all income levels)
  14. Error-Latin America & Caribbean (developing only)
  15. Error-Least developed countries: UN classification
  16. Error-Low & middle income
  17. Error-Low income
  18. Error-Lower middle income
  19. Error-Middle East & North Africa (all income levels)
  20. Error-Middle East & North Africa (developing only)
  21. Error-Middle income
  22. Error-North America
  23. Error-OECD members
  24. Error-Other small states
  25. Error-Pacific island small states
  26. Error-Small states
  27. Error-South Asia
  28. Error-Sub-Saharan Africa (all income levels)
  29. Error-Sub-Saharan Africa (developing only)
  30. Error-Upper middle income
  31. Error-World
  32. Error-Afghanistan
  33. Error-Albania
  34. Error-Algeria
  35. Error-American Samoa
  36. ad:84864
  37. Error-Angola
  38. Error-Antigua and Barbuda
  39. Error-Argentina
  40. Error-Armenia
  41. Error-Aruba
  42. Error-Australia
  43. Error-Austria
  44. Error-Azerbaijan
  45. Error-Bahamas, The
  46. Error-Bahrain
  47. Error-Bangladesh
  48. Error-Barbados
  49. Error-Belarus
  50. Error-Belgium
  51. Error-Belize
  52. Error-Benin
  53. Error-Bermuda
  54. Error-Bhutan
  55. Error-Bolivia
  56. Error-Bosnia and Herzegovina
  57. Error-Botswana
  58. Error-Brazil
  59. Error-Brunei Darussalam
  60. Error-Bulgaria
  61. Error-Burkina Faso
  62. Error-Burundi
  63. Error-Cambodia
  64. Error-Cameroon
  65. Error-Canada
  66. Error-Cape Verde
  67. Error-Cayman Islands
  68. Error-Central African Republic
  69. Error-Chad
  70. Error-Channel Islands
  71. Error-Chile
  72. Error-China
  73. Error-Colombia
  74. Error-Comoros
  75. Error-Congo, Dem. Rep.
  76. Error-Congo, Rep.
  77. Error-Costa Rica
  78. Error-Cote d'Ivoire
  79. Error-Croatia
  80. Error-Cuba
  81. Error-Curacao
  82. Error-Cyprus
  83. Error-Czech Republic
  84. Error-Denmark
  85. Error-Djibouti
  86. Error-Dominica
  87. Error-Dominican Republic
  88. Error-Ecuador
  89. Error-Egypt, Arab Rep.
  90. Error-El Salvador
  91. Error-Equatorial Guinea
  92. Error-Eritrea
  93. Error-Estonia
  94. Error-Ethiopia
  95. Error-Faeroe Islands
  96. Error-Fiji
  97. Error-Finland
  98. Error-France
  99. Error-French Polynesia
  100. Error-Gabon
  101. Error-Gambia, The
  102. Error-Georgia
  103. Error-Germany
  104. Error-Ghana
  105. Error-Gibraltar
  106. Error-Greece
  107. Error-Greenland
  108. Error-Grenada
  109. Error-Guam
  110. Error-Guatemala
  111. Error-Guinea
  112. Error-Guinea-Bissau
  113. Error-Guyana
  114. Error-Haiti
  115. Error-Honduras
  116. Error-Hong Kong SAR, China
  117. Error-Hungary
  118. Error-Iceland
  119. Error-India
  120. Error-Indonesia
  121. Error-Iran, Islamic Rep.
  122. Error-Iraq
  123. Error-Ireland
  124. Error-Isle of Man
  125. Error-Israel
  126. Error-Italy
  127. Error-Jamaica
  128. Error-Japan
  129. Error-Jordan
  130. Error-Kazakhstan
  131. Error-Kenya
  132. Error-Kiribati
  133. Error-Korea, Dem. Rep.
  134. Error-Korea, Rep.
  135. Error-Kosovo
  136. Error-Kuwait
  137. Error-Kyrgyz Republic
  138. Error-Lao PDR
  139. Error-Latvia
  140. Error-Lebanon
  141. Error-Lesotho
  142. Error-Liberia
  143. Error-Libya
  144. Error-Liechtenstein
  145. Error-Lithuania
  146. Error-Luxembourg
  147. Error-Macao SAR, China
  148. Error-Macedonia, FYR
  149. Error-Madagascar
  150. Error-Malawi
  151. Error-Malaysia
  152. Error-Maldives
  153. Error-Mali
  154. Error-Malta
  155. Error-Marshall Islands
  156. Error-Mauritania
  157. Error-Mauritius
  158. Error-Mayotte
  159. Error-Mexico
  160. Error-Micronesia, Fed. Sts.
  161. Error-Moldova
  162. Error-Monaco
  163. Error-Mongolia
  164. Error-Montenegro
  165. Error-Morocco
  166. Error-Mozambique
  167. Error-Myanmar
  168. Error-Namibia
  169. Error-Nepal
  170. Error-Netherlands
  171. Error-New Caledonia
  172. Error-New Zealand
  173. Error-Nicaragua
  174. Error-Niger
  175. Error-Nigeria
  176. Error-Northern Mariana Islands
  177. Error-Norway
  178. Error-Oman
  179. Error-Pakistan
  180. Error-Palau
  181. Error-Panama
  182. Error-Papua New Guinea
  183. Error-Paraguay
  184. Error-Peru
  185. Error-Philippines
  186. Error-Poland
  187. Error-Portugal
  188. Error-Puerto Rico
  189. Error-Qatar
  190. Error-Romania
  191. Error-Russian Federation
  192. Error-Rwanda
  193. Error-Samoa
  194. Error-San Marino
  195. Error-Sao Tome and Principe
  196. Error-Saudi Arabia
  197. Error-Senegal
  198. Error-Serbia
  199. Error-Seychelles
  200. Error-Sierra Leone
  201. Error-Singapore
  202. Error-Sint Maarten (Dutch part)
  203. Error-Slovak Republic
  204. Error-Slovenia
  205. Error-Solomon Islands
  206. Error-Somalia
  207. Error-South Africa
  208. Error-Spain
  209. Error-Sri Lanka
  210. Error-St. Kitts and Nevis
  211. Error-St. Lucia
  212. Error-St. Martin (French part)
  213. Error-St. Vincent and the Grenadines
  214. Error-Sudan
  215. Error-Suriname
  216. Error-Swaziland
  217. Error-Sweden
  218. Error-Switzerland
  219. Error-Syrian Arab Republic
  220. Error-Tajikistan
  221. Error-Tanzania
  222. Error-Thailand
  223. Error-Timor-Leste
  224. Error-Togo
  225. Error-Tonga
  226. Error-Trinidad and Tobago
  227. Error-Tunisia
  228. Error-Turkey
  229. Error-Turkmenistan
  230. Error-Turks and Caicos Islands
  231. Error-Tuvalu
  232. Error-Uganda
  233. Error-Ukraine
  234. Error-United Arab Emirates
  235. Error-United Kingdom
  236. Error-United States
  237. Error-Uruguay
  238. Error-Uzbekistan
  239. Error-Vanuatu
  240. Error-Venezuela, RB
  241. Error-Vietnam
  242. Error-Virgin Islands (U.S.)
  243. Error-West Bank and Gaza
  244. Error-Yemen, Rep.
  245. Error-Zambia
  246. Error-Zimbabwe
复制代码

就是执行后变成这样 有人可以指导一下吗 这其实是《python从入门到实践》的第十六章的示范可是我真的看不懂
回复

使用道具 举报

0

主题

9

帖子

9

积分

贫民

积分
9
ll104567i 发表于 2019-11-18 18:29:47 | 显示全部楼层
    from pygal_maps_world.maps import COUNTRIES

    def get_country_code(country_name):
        for code,name in COUNTRIES.items():
            if name==country_name:
                return code
            else:
                return None

问题出在这个函数上,在另一个帖子我解释了这个你看看,看不懂再问我吧
回复 支持 2 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表