[英]How can I pull specific attributes from a Python3 module?
Im importing from the pycountries module and trying to just print out a list of all the listed countries but I can figure it out. 我从pycountries模块导入,并尝试仅打印出所有列出的国家/地区的列表,但我可以弄清楚。 My test code is: 我的测试代码是:
from pycountry import countries
for num in range(0,len(countries)):
print(countries[num])
Which spits out a list of all the country objects (I'll list one for example). 这会列出所有国家对象的清单(例如,我列出一个)。
Country(alpha_2='AO', alpha_3='AGO', name='Angola', numeric='024', official_name='Republic of Angola')
I've tried looking at listed dir methods and tried things like: 我尝试查看列出的dir方法,并尝试了以下操作:
print(countries[num][name])
What am I doing wrong? 我究竟做错了什么? How to I pull specific things like the name or the alpha_2 from the object. 如何从对象中提取名称或alpha_2之类的特定内容。
Thanks for any help and sorry for the probably stupid question. 感谢您的帮助,对于可能很愚蠢的问题,我们深表歉意。
name
, alpha_2
, etc. are stored as an object attribute: name
, alpha_2
等存储为对象属性:
You can pull it out with : 您可以使用:
<country object>.alpha_2
or 要么
<country object>.name
You can cycle through all the countries with a loop: 您可以循环浏览所有国家/地区:
for country in countries:
print(country.name)
>>>"Afghanistan"
>>>....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.