问题
I try to find the name of a country through the name of a city. pycountry did the work for me!
import pycountry
pycountry.countries.search_fuzzy('beja')
Out[5]:
[Country(alpha_2='PT', alpha_3='PRT', name='Portugal', numeric='620', official_name='Portuguese Republic'),
Country(alpha_2='TN', alpha_3='TUN', name='Tunisia', numeric='788', official_name='Republic of Tunisia'),
Country(alpha_2='DZ', alpha_3='DZA', name='Algeria', numeric='012', official_name="People's Democratic Republic of Algeria")]
the problem is when I test somthing like pycountry.countries.search_fuzzy('hi')
it also returns countries names !
is there a way to prevent it ? something like a parameter to add which allows to increase the precision and the accuracy of the result
how can I find the name of a country through a name of a city
example:
import pycountry
sentence= "I live in paris"
words= sentence.split()
for s in words:
print(pycountry.countries.search_fuzzy(s))
I need something that returns to me France insteade of all this :
for s in words:
print(pycountry.countries.search_fuzzy(s))
[Country(alpha_2='GB', alpha_3='GBR', name='United Kingdom', numeric='826', official_name='United Kingdom of Great Britain and Northern Ireland'), Country(alpha_2='SI', alpha_3='SVN', name='Slovenia', numeric='705', official_name='Republic of Slovenia'), Country(alpha_2='IT', alpha_3='ITA', name='Italy', numeric='380', official_name='Italian Republic'), Country(alpha_2='GQ', alpha_3='GNQ', name='Equatorial Guinea', numeric='226', official_name='Republic of Equatorial Guinea'), Country(alpha_2='GR', alpha_3='GRC', name='Greece', numeric='300', official_name='Hellenic Republic'), Country(alpha_2='LV', alpha_3='LVA', name='Latvia', numeric='428', official_name='Republic of Latvia'), Country(alpha_2='PH', alpha_3='PHL', name='Philippines', numeric='608', official_name='Republic of the Philippines'), Country(alpha_2='FR', alpha_3='FRA', name='France', numeric='250', official_name='French Republic'), Country(alpha_2='MA', alpha_3='MAR', name='Morocco', numeric='504', official_name='Kingdom of Morocco'), Country(alpha_2='VN', alpha_3='VNM', common_name='Vietnam', name='Viet Nam', numeric='704', official_name='Socialist Republic of Viet Nam'), Country(alpha_2='TR', alpha_3='TUR', name='Turkey', numeric='792', official_name='Republic of Turkey'), Country(alpha_2='US', alpha_3='USA', name='United States', numeric='840', official_name='United States of America'), Country(alpha_2='UG', alpha_3='UGA', name='Uganda', numeric='800', official_name='Republic of Uganda'), Country(alpha_2='CZ', alpha_3='CZE', name='Czechia', numeric='203', official_name='Czech Republic'), Country(alpha_2='CN', alpha_3='CHN', name='China', numeric='156', official_name="People's Republic of China"), Country(alpha_2='MK', alpha_3='MKD', name='North Macedonia', numeric='807', official_name='Republic of North Macedonia'), Country(alpha_2='DZ', alpha_3='DZA', name='Algeria', numeric='012', official_name="People's Democratic Republic of Algeria"), Country(alpha_2='MT', alpha_3='MLT', name='Malta', numeric='470', official_name='Republic of Malta'), Country(alpha_2='RU', alpha_3='RUS', name='Russian Federation', numeric='643'), Country(alpha_2='ES', alpha_3='ESP', name='Spain', numeric='724', official_name='Kingdom of Spain'), Country(alpha_2='JP', alpha_3='JPN', name='Japan', numeric='392'), Country(alpha_2='TH', alpha_3='THA', name='Thailand', numeric='764', official_name='Kingdom of Thailand'), Country(alpha_2='BD', alpha_3='BGD', name='Bangladesh', numeric='050', official_name="People's Republic of Bangladesh"), Country(alpha_2='TW', alpha_3='TWN', common_name='Taiwan', name='Taiwan, Province of China', numeric='158', official_name='Taiwan, Province of China'), Country(alpha_2='KN', alpha_3='KNA', name='Saint Kitts and Nevis', numeric='659'), Country(alpha_2='MD', alpha_3='MDA', common_name='Moldova', name='Moldova, Republic of', numeric='498', official_name='Republic of Moldova'), Country(alpha_2='RO', alpha_3='ROU', name='Romania', numeric='642'), Country(alpha_2='AL', alpha_3='ALB', name='Albania', numeric='008', official_name='Republic of Albania'), Country(alpha_2='RS', alpha_3='SRB', name='Serbia', numeric='688', official_name='Republic of Serbia'), Country(alpha_2='IN', alpha_3='IND', name='India', numeric='356', official_name='Republic of India'), Country(alpha_2='MX', alpha_3='MEX', name='Mexico', numeric='484', official_name='United Mexican States'), Country(alpha_2='IE', alpha_3='IRL', name='Ireland', numeric='372'), Country(alpha_2='AZ', alpha_3='AZE', name='Azerbaijan', numeric='031', official_name='Republic of Azerbaijan'), Country(alpha_2='GN', alpha_3='GIN', name='Guinea', numeric='324', official_name='Republic of Guinea'), Country(alpha_2='HR', alpha_3='HRV', name='Croatia', numeric='191', official_name='Republic of Croatia'), Country(alpha_2='NG', alpha_3='NGA', name='Nigeria', numeric='566', official_name='Federal Republic of Nigeria'), Country(alpha_2='BR', alpha_3='BRA', name='Brazil', numeric='076', official_name='Federative Republic of Brazil'), Country(alpha_2='ID', alpha_3='IDN', name='Indonesia', numeric='360', official_name='Republic of Indonesia'), Country(alpha_2='UA', alpha_3='UKR', name='Ukraine', numeric='804'), Country(alpha_2='CV', alpha_3='CPV', name='Cabo Verde', numeric='132', official_name='Republic of Cabo Verde'), Country(alpha_2='DM', alpha_3='DMA', name='Dominica', numeric='212', official_name='Commonwealth of Dominica'), Country(alpha_2='BF', alpha_3='BFA', name='Burkina Faso', numeric='854'), Country(alpha_2='IR', alpha_3='IRN', name='Iran, Islamic Republic of', numeric='364', official_name='Islamic Republic of Iran'), Country(alpha_2='TZ', alpha_3='TZA', common_name='Tanzania', name='Tanzania, United Republic of', numeric='834', official_name='United Republic of Tanzania'), Country(alpha_2='CO', alpha_3='COL', name='Colombia', numeric='170', official_name='Republic of Colombia'), Country(alpha_2='IQ', alpha_3='IRQ', name='Iraq', numeric='368', official_name='Republic of Iraq'), Country(alpha_2='MW', alpha_3='MWI', name='Malawi', numeric='454', official_name='Republic of Malawi'), Country(alpha_2='TL', alpha_3='TLS', name='Timor-Leste', numeric='626', official_name='Democratic Republic of Timor-Leste'), Country(alpha_2='TN', alpha_3='TUN', name='Tunisia', numeric='788', official_name='Republic of Tunisia'), Country(alpha_2='TT', alpha_3='TTO', name='Trinidad and Tobago', numeric='780', official_name='Republic of Trinidad and Tobago'), Country(alpha_2='NL', alpha_3='NLD', name='Netherlands', numeric='528', official_name='Kingdom of the Netherlands'), Country(alpha_2='TD', alpha_3='TCD', name='Chad', numeric='148', official_name='Republic of Chad'), Country(alpha_2='CL', alpha_3='CHL', name='Chile', numeric='152', official_name='Republic of Chile'), Country(alpha_2='NI', alpha_3='NIC', name='Nicaragua', numeric='558', official_name='Republic of Nicaragua'), Country(alpha_2='FI', alpha_3='FIN', name='Finland', numeric='246', official_name='Republic of Finland'), Country(alpha_2='JM', alpha_3='JAM', name='Jamaica', numeric='388'), Country(alpha_2='MH', alpha_3='MHL', name='Marshall Islands', numeric='584', official_name='Republic of the Marshall Islands'), Country(alpha_2='CU', alpha_3='CUB', name='Cuba', numeric='192', official_name='Republic of Cuba'), Country(alpha_2='DO', alpha_3='DOM', name='Dominican Republic', numeric='214'), Country(alpha_2='LT', alpha_3='LTU', name='Lithuania', numeric='440', official_name='Republic of Lithuania'), Country(alpha_2='NE', alpha_3='NER', name='Niger', numeric='562', official_name='Republic of the Niger'), Country(alpha_2='CH', alpha_3='CHE', name='Switzerland', numeric='756', official_name='Swiss Confederation'), Country(alpha_2='ET', alpha_3='ETH', name='Ethiopia', numeric='231', official_name='Federal Democratic Republic of Ethiopia'), Country(alpha_2='LR', alpha_3='LBR', name='Liberia', numeric='430', official_name='Republic of Liberia'), Country(alpha_2='SC', alpha_3='SYC', name='Seychelles', numeric='690', official_name='Republic of Seychelles'), Country(alpha_2='AG', alpha_3='ATG', name='Antigua and Barbuda', numeric='028'), Country(alpha_2='BG', alpha_3='BGR', name='Bulgaria', numeric='100', official_name='Republic of Bulgaria'), Country(alpha_2='GW', alpha_3='GNB', name='Guinea-Bissau', numeric='624', official_name='Republic of Guinea-Bissau'), Country(alpha_2='PE', alpha_3='PER', name='Peru', numeric='604', official_name='Republic of Peru'), Country(alpha_2='WS', alpha_3='WSM', name='Samoa', numeric='882', official_name='Independent State of Samoa'), Country(alpha_2='EC', alpha_3='ECU', name='Ecuador', numeric='218', official_name='Republic of Ecuador'), Country(alpha_2='GE', alpha_3='GEO', name='Georgia', numeric='268'), Country(alpha_2='LY', alpha_3='LBY', name='Libya', numeric='434', official_name='Libya'), Country(alpha_2='PK', alpha_3='PAK', name='Pakistan', numeric='586', official_name='Islamic Republic of Pakistan'), Country(alpha_2='AR', alpha_3='ARG', name='Argentina', numeric='032', official_name='Argentine Republic'), Country(alpha_2='NR', alpha_3='NRU', name='Nauru', numeric='520', official_name='Republic of Nauru'), Country(alpha_2='PL', alpha_3='POL', name='Poland', numeric='616', official_name='Republic of Poland'), Country(alpha_2='SY', alpha_3='SYR', name='Syrian Arab Republic', numeric='760'), Country(alpha_2='UM', alpha_3='UMI', name='United States Minor Outlying Islands', numeric='581'), Country(alpha_2='VC', alpha_3='VCT', name='Saint Vincent and the Grenadines', numeric='670'), Country(alpha_2='AO', alpha_3='AGO', name='Angola', numeric='024', official_name='Republic of Angola'), Country(alpha_2='CI', alpha_3='CIV', name="Côte d'Ivoire", numeric='384', official_name="Republic of Côte d'Ivoire"), Country(alpha_2='LK', alpha_3='LKA', name='Sri Lanka', numeric='144', official_name='Democratic Socialist Republic of Sri Lanka'), Country(alpha_2='MC', alpha_3='MCO', name='Monaco', numeric='492', official_name='Principality of Monaco'), Country(alpha_2='SR', alpha_3='SUR', name='Suriname', numeric='740', official_name='Republic of Suriname'), Country(alpha_2='GT', alpha_3='GTM', name='Guatemala', numeric='320', official_name='Republic of Guatemala'), Country(alpha_2='KI', alpha_3='KIR', name='Kiribati', numeric='296', official_name='Republic of Kiribati'), Country(alpha_2='MR', alpha_3='MRT', name='Mauritania', numeric='478', official_name='Islamic Republic of Mauritania'), Country(alpha_2='BI', alpha_3='BDI', name='Burundi', numeric='108', official_name='Republic of Burundi'), Country(alpha_2='BT', alpha_3='BTN', name='Bhutan', numeric='064', official_name='Kingdom of Bhutan'), Country(alpha_2='DJ', alpha_3='DJI', name='Djibouti', numeric='262', official_name='Republic of Djibouti'), Country(alpha_2='MU', alpha_3='MUS', name='Mauritius', numeric='480', official_name='Republic of Mauritius'), Country(alpha_2='PY', alpha_3='PRY', name='Paraguay', numeric='600', official_name='Republic of Paraguay'), Country(alpha_2='SN', alpha_3='SEN', name='Senegal', numeric='686', official_name='Republic of Senegal'), Country(alpha_2='AT', alpha_3='AUT', name='Austria', numeric='040', official_name='Republic of Austria'), Country(alpha_2='BA', alpha_3='BIH', name='Bosnia and Herzegovina', numeric='070', official_name='Republic of Bosnia and Herzegovina'), Country(alpha_2='BE', alpha_3='BEL', name='Belgium', numeric='056', official_name='Kingdom of Belgium'), Country(alpha_2='LI', alpha_3='LIE', name='Liechtenstein', numeric='438', official_name='Principality of Liechtenstein'), Country(alpha_2='SA', alpha_3='SAU', name='Saudi Arabia', numeric='682', official_name='Kingdom of Saudi Arabia'), Country(alpha_2='ZW', alpha_3='ZWE', name='Zimbabwe', numeric='716', official_name='Republic of Zimbabwe'), Country(alpha_2='EE', alpha_3='EST', name='Estonia', numeric='233', official_name='Republic of Estonia'), Country(alpha_2='ML', alpha_3='MLI', name='Mali', numeric='466', official_name='Republic of Mali'), Country(alpha_2='NO', alpha_3='NOR', name='Norway', numeric='578', official_name='Kingdom of Norway'), Country(alpha_2='BJ', alpha_3='BEN', name='Benin', numeric='204', official_name='Republic of Benin'), Country(alpha_2='HN', alpha_3='HND', name='Honduras', numeric='340', official_name='Republic of Honduras'), Country(alpha_2='IS', alpha_3='ISL', name='Iceland', numeric='352', official_name='Republic of Iceland'), Country(alpha_2='MV', alpha_3='MDV', name='Maldives', numeric='462', official_name='Republic of Maldives'), Country(alpha_2='MY', alpha_3='MYS', name='Malaysia', numeric='458'), Country(alpha_2='PT', alpha_3='PRT', name='Portugal', numeric='620', official_name='Portuguese Republic'), Country(alpha_2='SH', alpha_3='SHN', name='Saint Helena, Ascension and Tristan da Cunha', numeric='654'), Country(alpha_2='TO', alpha_3='TON', name='Tonga', numeric='776', official_name='Kingdom of Tonga'), Country(alpha_2='BB', alpha_3='BRB', name='Barbados', numeric='052'), Country(alpha_2='DK', alpha_3='DNK', name='Denmark', numeric='208', official_name='Kingdom of Denmark'), Country(alpha_2='IL', alpha_3='ISR', name='Israel', numeric='376', official_name='State of Israel'), Country(alpha_2='PW', alpha_3='PLW', name='Palau', numeric='585', official_name='Republic of Palau'), Country(alpha_2='UZ', alpha_3='UZB', name='Uzbekistan', numeric='860', official_name='Republic of Uzbekistan'), Country(alpha_2='AD', alpha_3='AND', name='Andorra', numeric='020', official_name='Principality of Andorra'), Country(alpha_2='AF', alpha_3='AFG', name='Afghanistan', numeric='004', official_name='Islamic Republic of Afghanistan'), Country(alpha_2='EG', alpha_3='EGY', name='Egypt', numeric='818', official_name='Arab Republic of Egypt'), Country(alpha_2='ER', alpha_3='ERI', name='Eritrea', numeric='232', official_name='the State of Eritrea'), Country(alpha_2='LS', alpha_3='LSO', name='Lesotho', numeric='426', official_name='Kingdom of Lesotho'), Country(alpha_2='MZ', alpha_3='MOZ', name='Mozambique', numeric='508', official_name='Republic of Mozambique'), Country(alpha_2='SK', alpha_3='SVK', name='Slovakia', numeric='703', official_name='Slovak Republic'), Country(alpha_2='IM', alpha_3='IMN', name='Isle of Man', numeric='833'), Country(alpha_2='SD', alpha_3='SDN', name='Sudan', numeric='729', official_name='Republic of the Sudan'), Country(alpha_2='SE', alpha_3='SWE', name='Sweden', numeric='752', official_name='Kingdom of Sweden'), Country(alpha_2='YE', alpha_3='YEM', name='Yemen', numeric='887', official_name='Republic of Yemen'), Country(alpha_2='BO', alpha_3='BOL', common_name='Bolivia', name='Bolivia, Plurinational State of', numeric='068', official_name='Plurinational State of Bolivia'), Country(alpha_2='BY', alpha_3='BLR', name='Belarus', numeric='112', official_name='Republic of Belarus'), Country(alpha_2='CG', alpha_3='COG', name='Congo', numeric='178', official_name='Republic of the Congo'), Country(alpha_2='FM', alpha_3='FSM', name='Micronesia, Federated States of', numeric='583', official_name='Federated States of Micronesia'), Country(alpha_2='SG', alpha_3='SGP', name='Singapore', numeric='702', official_name='Republic of Singapore'), Country(alpha_2='SL', alpha_3='SLE', name='Sierra Leone', numeric='694', official_name='Republic of Sierra Leone'), Country(alpha_2='AE', alpha_3='ARE', name='United Arab Emirates', numeric='784'), Country(alpha_2='FJ', alpha_3='FJI', name='Fiji', numeric='242', official_name='Republic of Fiji'), Country(alpha_2='GI', alpha_3='GIB', name='Gibraltar', numeric='292'), Country(alpha_2='GY', alpha_3='GUY', name='Guyana', numeric='328', official_name='Republic of Guyana'), Country(alpha_2='HT', alpha_3='HTI', name='Haiti', numeric='332', official_name='Republic of Haiti'), Country(alpha_2='KH', alpha_3='KHM', name='Cambodia', numeric='116', official_name='Kingdom of Cambodia'), Country(alpha_2='KM', alpha_3='COM', name='Comoros', numeric='174', official_name='Union of the Comoros'), Country(alpha_2='MM', alpha_3='MMR', name='Myanmar', numeric='104', official_name='Republic of Myanmar'), Country(alpha_2='NA', alpha_3='NAM', name='Namibia', numeric='516', official_name='Republic of Namibia'), Country(alpha_2='NU', alpha_3='NIU', name='Niue', numeric='570', official_name='Niue'), Country(alpha_2='PN', alpha_3='PCN', name='Pitcairn', numeric='612'), Country(alpha_2='PS', alpha_3='PSE', name='Palestine, State of', numeric='275', official_name='the State of Palestine'), Country(alpha_2='SO', alpha_3='SOM', name='Somalia', numeric='706', official_name='Federal Republic of Somalia'), Country(alpha_2='SX', alpha_3='SXM', name='Sint Maarten (Dutch part)', numeric='534', official_name='Sint Maarten (Dutch part)'), Country(alpha_2='VG', alpha_3='VGB', name='Virgin Islands, British', numeric='092', official_name='British Virgin Islands'), Country(alpha_2='VI', alpha_3='VIR', name='Virgin Islands, U.S.', numeric='850', official_name='Virgin Islands of the United States'), Country(alpha_2='AM', alpha_3='ARM', name='Armenia', numeric='051', official_name='Republic of Armenia'), Country(alpha_2='BQ', alpha_3='BES', name='Bonaire, Sint Eustatius and Saba', numeric='535', official_name='Bonaire, Sint Eustatius and Saba'), Country(alpha_2='KE', alpha_3='KEN', name='Kenya', numeric='404', official_name='Republic of Kenya'), Country(alpha_2='KW', alpha_3='KWT', name='Kuwait', numeric='414', official_name='State of Kuwait'), Country(alpha_2='LA', alpha_3='LAO', name="Lao People's Democratic Republic", numeric='418'), Country(alpha_2='MG', alpha_3='MDG', name='Madagascar', numeric='450', official_name='Republic of Madagascar'), Country(alpha_2='MN', alpha_3='MNG', name='Mongolia', numeric='496'), Country(alpha_2='SM', alpha_3='SMR', name='San Marino', numeric='674', official_name='Republic of San Marino'), Country(alpha_2='BL', alpha_3='BLM', name='Saint Barthélemy', numeric='652'), Country(alpha_2='BZ', alpha_3='BLZ', name='Belize', numeric='084'), Country(alpha_2='IO', alpha_3='IOT', name='British Indian Ocean Territory', numeric='086'), Country(alpha_2='LC', alpha_3='LCA', name='Saint Lucia', numeric='662'), Country(alpha_2='MF', alpha_3='MAF', name='Saint Martin (French part)', numeric='663'), Country(alpha_2='PM', alpha_3='SPM', name='Saint Pierre and Miquelon', numeric='666'), Country(alpha_2='SB', alpha_3='SLB', name='Solomon Islands', numeric='090'), Country(alpha_2='SS', alpha_3='SSD', name='South Sudan', numeric='728', official_name='Republic of South Sudan'), Country(alpha_2='VE', alpha_3='VEN', common_name='Venezuela', name='Venezuela, Bolivarian Republic of', numeric='862', official_name='Bolivarian Republic of Venezuela'), Country(alpha_2='AU', alpha_3='AUS', name='Australia', numeric='036'), Country(alpha_2='GM', alpha_3='GMB', name='Gambia', numeric='270', official_name='Republic of the Gambia'), Country(alpha_2='JO', alpha_3='JOR', name='Jordan', numeric='400', official_name='Hashemite Kingdom of Jordan'), Country(alpha_2='CX', alpha_3='CXR', name='Christmas Island', numeric='162'), Country(alpha_2='ME', alpha_3='MNE', name='Montenegro', numeric='499', official_name='Montenegro'), Country(alpha_2='SZ', alpha_3='SWZ', name='Eswatini', numeric='748', official_name='Kingdom of Eswatini'), Country(alpha_2='TJ', alpha_3='TJK', name='Tajikistan', numeric='762', official_name='Republic of Tajikistan'), Country(alpha_2='BH', alpha_3='BHR', name='Bahrain', numeric='048', official_name='Kingdom of Bahrain'), Country(alpha_2='BS', alpha_3='BHS', name='Bahamas', numeric='044', official_name='Commonwealth of the Bahamas'), Country(alpha_2='PA', alpha_3='PAN', name='Panama', numeric='591', official_name='Republic of Panama'), Country(alpha_2='PG', alpha_3='PNG', name='Papua New Guinea', numeric='598', official_name='Independent State of Papua New Guinea'), Country(alpha_2='ZM', alpha_3='ZMB', name='Zambia', numeric='894', official_name='Republic of Zambia'), Country(alpha_2='AI', alpha_3='AIA', name='Anguilla', numeric='660'), Country(alpha_2='AS', alpha_3='ASM', name='American Samoa', numeric='016'), Country(alpha_2='BN', alpha_3='BRN', name='Brunei Darussalam', numeric='096'), Country(alpha_2='CM', alpha_3='CMR', name='Cameroon', numeric='120', official_name='Republic of Cameroon'), Country(alpha_2='MQ', alpha_3='MTQ', name='Martinique', numeric='474'), Country(alpha_2='NP', alpha_3='NPL', name='Nepal', numeric='524', official_name='Federal Democratic Republic of Nepal'), Country(alpha_2='RE', alpha_3='REU', name='Réunion', numeric='638'), Country(alpha_2='SV', alpha_3='SLV', name='El Salvador', numeric='222', official_name='Republic of El Salvador'), Country(alpha_2='UY', alpha_3='URY', name='Uruguay', numeric='858', official_name='Eastern Republic of Uruguay'), Country(alpha_2='WF', alpha_3='WLF', name='Wallis and Futuna', numeric='876'), Country(alpha_2='CR', alpha_3='CRI', name='Costa Rica', numeric='188', official_name='Republic of Costa Rica'), Country(alpha_2='BW', alpha_3='BWA', name='Botswana', numeric='072', official_name='Republic of Botswana'), Country(alpha_2='CK', alpha_3='COK', name='Cook Islands', numeric='184'), Country(alpha_2='CY', alpha_3='CYP', name='Cyprus', numeric='196', official_name='Republic of Cyprus'), Country(alpha_2='KZ', alpha_3='KAZ', name='Kazakhstan', numeric='398', official_name='Republic of Kazakhstan'), Country(alpha_2='GD', alpha_3='GRD', name='Grenada', numeric='308'), Country(alpha_2='GH', alpha_3='GHA', name='Ghana', numeric='288', official_name='Republic of Ghana'), Country(alpha_2='AX', alpha_3='ALA', name='Åland Islands', numeric='248'), Country(alpha_2='CD', alpha_3='COD', name='Congo, The Democratic Republic of the', numeric='180'), Country(alpha_2='FO', alpha_3='FRO', name='Faroe Islands', numeric='234'), Country(alpha_2='HM', alpha_3='HMD', name='Heard Island and McDonald Islands', numeric='334'), Country(alpha_2='VU', alpha_3='VUT', name='Vanuatu', numeric='548', official_name='Republic of Vanuatu'), Country(alpha_2='KR', alpha_3='KOR', name='Korea, Republic of', numeric='410'), Country(alpha_2='ZA', alpha_3='ZAF', name='South Africa', numeric='710', official_name='Republic of South Africa'), Country(alpha_2='AQ', alpha_3='ATA', name='Antarctica', numeric='010'), , , ...
I also tried the solution from here How to extract countries from a text? but it didn't work for me
回答1:
try this:
import spacy
import pycountry
nlp = spacy.load("en_core_web_sm")
sentence= nlp("I live in Paris")
for ent in sentence.ents:
print(ent.text)
print((pycountry.countries.search_fuzzy(ent.text))[0].name)
it returns :
Paris
France
来源:https://stackoverflow.com/questions/62396099/pycountry-didnt-return-correct-value