How to convert timezone abbrevation to full form - python

I am using pyzt to get all timezones params from the name for example Asia/Tokyo, I can get JST but I want to convert it to Japan Standard Time. Is there any way in pyzt to do it this or some other way?
Timezone name: Asia/Tokyo
Timezone abbrevation: JST
Need it to give "Japan Standard Time"

The method to get a timezone's full/long name is pytz.country_timezones[country_code], so in your case, you want to suppy 'JP' as country_code. For more information, see pytz documentation.
Since you did not supply any code example, I cannot tell if this answer is enough to help you. Let me know if you need more help and I'll edit this answer accordingly.

I'm not sure it's possible with pytz but it's easy to build a mapping dict with Pandas (or reuse the dictionary at the bottom):
import pandas as pd
df = pd.read_html('https://www.timeanddate.com/time/zones/')[0].iloc[:, :2]
df['Time zone name'] = df['Time zone name'].str.split('\s\s+').str[0]
dmap = df.set_index('Abbreviation')['Time zone name'].to_dict()
Usage:
>>> dmap['JST']
'Japan Standard Time'
>>> dmap['CEST']
'Central European Summer Time'
>>> dmap['PDT']
'Pacific Daylight Time'
dmap:
dmap = {'A': 'Alpha Time Zone',
'ACDT': 'Australian Central Daylight Time',
'ACST': 'Australian Central Standard Time',
'ACT': 'Australian Central Time',
'ACWST': 'Australian Central Western Standard Time',
'ADT': 'Atlantic Daylight Time',
'AEDT': 'Australian Eastern Daylight Time',
'AEST': 'Australian Eastern Standard Time',
'AET': 'Australian Eastern Time',
'AFT': 'Afghanistan Time',
'AKDT': 'Alaska Daylight Time',
'AKST': 'Alaska Standard Time',
'ALMT': 'Alma-Ata Time',
'AMST': 'Armenia Summer Time',
'AMT': 'Armenia Time',
'ANAST': 'Anadyr Summer Time',
'ANAT': 'Anadyr Time',
'AQTT': 'Aqtobe Time',
'ART': 'Argentina Time',
'AST': 'Atlantic Standard Time',
'AT': 'Atlantic Time',
'AWDT': 'Australian Western Daylight Time',
'AWST': 'Australian Western Standard Time',
'AZOST': 'Azores Summer Time',
'AZOT': 'Azores Time',
'AZST': 'Azerbaijan Summer Time',
'AZT': 'Azerbaijan Time',
'AoE': 'Anywhere on Earth',
'B': 'Bravo Time Zone',
'BNT': 'Brunei Darussalam Time',
'BOT': 'Bolivia Time',
'BRST': 'Brasília Summer Time',
'BRT': 'Brasília Time',
'BST': 'British Summer Time',
'BTT': 'Bhutan Time',
'C': 'Charlie Time Zone',
'CAST': 'Casey Time',
'CAT': 'Central Africa Time',
'CCT': 'Cocos Islands Time',
'CDT': 'Cuba Daylight Time',
'CEST': 'Central European Summer Time',
'CET': 'Central European Time',
'CHADT': 'Chatham Island Daylight Time',
'CHAST': 'Chatham Island Standard Time',
'CHOST': 'Choibalsan Summer Time',
'CHOT': 'Choibalsan Time',
'CHUT': 'Chuuk Time',
'CIDST': 'Cayman Islands Daylight Saving Time',
'CIST': 'Cayman Islands Standard Time',
'CKT': 'Cook Island Time',
'CLST': 'Chile Summer Time',
'CLT': 'Chile Standard Time',
'COT': 'Colombia Time',
'CST': 'Cuba Standard Time',
'CT': 'Central Time',
'CVT': 'Cape Verde Time',
'CXT': 'Christmas Island Time',
'ChST': 'Chamorro Standard Time',
'D': 'Delta Time Zone',
'DAVT': 'Davis Time',
'DDUT': "Dumont-d'Urville Time",
'E': 'Echo Time Zone',
'EASST': 'Easter Island Summer Time',
'EAST': 'Easter Island Standard Time',
'EAT': 'Eastern Africa Time',
'ECT': 'Ecuador Time',
'EDT': 'Eastern Daylight Time',
'EEST': 'Eastern European Summer Time',
'EET': 'Eastern European Time',
'EGST': 'Eastern Greenland Summer Time',
'EGT': 'East Greenland Time',
'EST': 'Eastern Standard Time',
'ET': 'Eastern Time',
'F': 'Foxtrot Time Zone',
'FET': 'Further-Eastern European Time',
'FJST': 'Fiji Summer Time',
'FJT': 'Fiji Time',
'FKST': 'Falkland Islands Summer Time',
'FKT': 'Falkland Island Time',
'FNT': 'Fernando de Noronha Time',
'G': 'Golf Time Zone',
'GALT': 'Galapagos Time',
'GAMT': 'Gambier Time',
'GET': 'Georgia Standard Time',
'GFT': 'French Guiana Time',
'GILT': 'Gilbert Island Time',
'GMT': 'Greenwich Mean Time',
'GST': 'South Georgia Time',
'GYT': 'Guyana Time',
'H': 'Hotel Time Zone',
'HDT': 'Hawaii-Aleutian Daylight Time',
'HKT': 'Hong Kong Time',
'HOVST': 'Hovd Summer Time',
'HOVT': 'Hovd Time',
'HST': 'Hawaii Standard Time',
'I': 'India Time Zone',
'ICT': 'Indochina Time',
'IDT': 'Israel Daylight Time',
'IOT': 'Indian Chagos Time',
'IRDT': 'Iran Daylight Time',
'IRKST': 'Irkutsk Summer Time',
'IRKT': 'Irkutsk Time',
'IRST': 'Iran Standard Time',
'IST': 'Israel Standard Time',
'JST': 'Japan Standard Time',
'K': 'Kilo Time Zone',
'KGT': 'Kyrgyzstan Time',
'KOST': 'Kosrae Time',
'KRAST': 'Krasnoyarsk Summer Time',
'KRAT': 'Krasnoyarsk Time',
'KST': 'Korea Standard Time',
'KUYT': 'Kuybyshev Time',
'L': 'Lima Time Zone',
'LHDT': 'Lord Howe Daylight Time',
'LHST': 'Lord Howe Standard Time',
'LINT': 'Line Islands Time',
'M': 'Mike Time Zone',
'MAGST': 'Magadan Summer Time',
'MAGT': 'Magadan Time',
'MART': 'Marquesas Time',
'MAWT': 'Mawson Time',
'MDT': 'Mountain Daylight Time',
'MHT': 'Marshall Islands Time',
'MMT': 'Myanmar Time',
'MSD': 'Moscow Daylight Time',
'MSK': 'Moscow Standard Time',
'MST': 'Mountain Standard Time',
'MT': 'Mountain Time',
'MUT': 'Mauritius Time',
'MVT': 'Maldives Time',
'MYT': 'Malaysia Time',
'N': 'November Time Zone',
'NCT': 'New Caledonia Time',
'NDT': 'Newfoundland Daylight Time',
'NFDT': 'Norfolk Daylight Time',
'NFT': 'Norfolk Time',
'NOVST': 'Novosibirsk Summer Time',
'NOVT': 'Novosibirsk Time',
'NPT': 'Nepal Time',
'NRT': 'Nauru Time',
'NST': 'Newfoundland Standard Time',
'NUT': 'Niue Time',
'NZDT': 'New Zealand Daylight Time',
'NZST': 'New Zealand Standard Time',
'O': 'Oscar Time Zone',
'OMSST': 'Omsk Summer Time',
'OMST': 'Omsk Standard Time',
'ORAT': 'Oral Time',
'P': 'Papa Time Zone',
'PDT': 'Pacific Daylight Time',
'PET': 'Peru Time',
'PETST': 'Kamchatka Summer Time',
'PETT': 'Kamchatka Time',
'PGT': 'Papua New Guinea Time',
'PHOT': 'Phoenix Island Time',
'PHT': 'Philippine Time',
'PKT': 'Pakistan Standard Time',
'PMDT': 'Pierre & Miquelon Daylight Time',
'PMST': 'Pierre & Miquelon Standard Time',
'PONT': 'Pohnpei Standard Time',
'PST': 'Pitcairn Standard Time',
'PT': 'Pacific Time',
'PWT': 'Palau Time',
'PYST': 'Paraguay Summer Time',
'PYT': 'Pyongyang Time',
'Q': 'Quebec Time Zone',
'QYZT': 'Qyzylorda Time',
'R': 'Romeo Time Zone',
'RET': 'Reunion Time',
'ROTT': 'Rothera Time',
'S': 'Sierra Time Zone',
'SAKT': 'Sakhalin Time',
'SAMT': 'Samara Time',
'SAST': 'South Africa Standard Time',
'SBT': 'Solomon Islands Time',
'SCT': 'Seychelles Time',
'SGT': 'Singapore Time',
'SRET': 'Srednekolymsk Time',
'SRT': 'Suriname Time',
'SST': 'Samoa Standard Time',
'SYOT': 'Syowa Time',
'T': 'Tango Time Zone',
'TAHT': 'Tahiti Time',
'TFT': 'French Southern and Antarctic Time',
'TJT': 'Tajikistan Time',
'TKT': 'Tokelau Time',
'TLT': 'East Timor Time',
'TMT': 'Turkmenistan Time',
'TOST': 'Tonga Summer Time',
'TOT': 'Tonga Time',
'TRT': 'Turkey Time',
'TVT': 'Tuvalu Time',
'U': 'Uniform Time Zone',
'ULAST': 'Ulaanbaatar Summer Time',
'ULAT': 'Ulaanbaatar Time',
'UTC': 'Coordinated Universal Time',
'UYST': 'Uruguay Summer Time',
'UYT': 'Uruguay Time',
'UZT': 'Uzbekistan Time',
'V': 'Victor Time Zone',
'VET': 'Venezuelan Standard Time',
'VLAST': 'Vladivostok Summer Time',
'VLAT': 'Vladivostok Time',
'VOST': 'Vostok Time',
'VUT': 'Vanuatu Time',
'W': 'Whiskey Time Zone',
'WAKT': 'Wake Time',
'WARST': 'Western Argentine Summer Time',
'WAST': 'West Africa Summer Time',
'WAT': 'West Africa Time',
'WEST': 'Western European Summer Time',
'WET': 'Western European Time',
'WFT': 'Wallis and Futuna Time',
'WGST': 'Western Greenland Summer Time',
'WGT': 'West Greenland Time',
'WIB': 'Western Indonesian Time',
'WIT': 'Eastern Indonesian Time',
'WITA': 'Central Indonesian Time',
'WST': 'Western Sahara Summer Time',
'WT': 'Western Sahara Standard Time',
'X': 'X-ray Time Zone',
'Y': 'Yankee Time Zone',
'YAKST': 'Yakutsk Summer Time',
'YAKT': 'Yakutsk Time',
'YAPT': 'Yap Time',
'YEKST': 'Yekaterinburg Summer Time',
'YEKT': 'Yekaterinburg Time',
'Z': 'Zulu Time Zone'}

Related

Unable to use pycountry search_fuzzy to look up the alpha_3 based on given countries names, any better ideas?

I have the following 122 countries which I couldn't look up their corresponding alpha 3 code. I tried search_fuzzy but nothing is found.
By looking at some of the countries names, I can "manually" assign the alpha 3 based on common knowledge (such like creating a dic for rename). However, I wonder if there is a better way to look up the alpha 3 in an automated way, such like by using other function from pycoutry or even re?
Any suggestions and advice are greatly appreciated.
import pandas as pd
import numpy as np
import regex as re
import pycountry
missing = ['Americas', 'Asia', 'Australia and New Zealand', 'Bolivia (Plurinational State of)', 'Caribbean', 'Central America', 'Central and Southern Asia', 'Central Asia', 'China, Hong Kong Special Administrative Region', 'China, Macao Special Administrative Region', 'Democratic Republic of the Congo', 'Eastern Africa', 'Eastern and South-Eastern Asia', 'Eastern Asia', 'Eastern Europe', 'Europe', 'Europe and Northern America', 'Iran (Islamic Republic of)', 'Landlocked developing countries (LLDCs)', 'Latin America and the Caribbean', 'Least Developed Countries (LDCs)', 'Melanesia', 'Micronesia (Federated States of)', 'Middle Africa', 'Northern Africa', 'Northern Africa and Western Asia', 'Northern America', 'Northern Europe', 'Oceania', 'Oceania (exc. Australia and New Zealand)', 'Small island developing States (SIDS)', 'South America', 'South-Eastern Asia', 'Southern Africa', 'Southern Asia', 'Southern Europe', 'Sub-Saharan Africa', 'Türkiye', 'Venezuela (Bolivarian Republic of)', 'Western Africa', 'Western Asia', 'Western Europe', 'World', 'European Union (27)', 'Chinese Taipei', 'UAE', 'Belgium-Luxembourg', 'Channel Islands', 'China, Hong Kong SAR', 'China, Macao SAR', 'China, mainland', 'China, Taiwan Province of', 'Czechoslovakia', 'Ethiopia PDR', 'French Guyana', 'Netherlands Antilles (former)', 'Pacific Islands Trust Territory', 'Serbia and Montenegro', 'Sudan (former)', 'Svalbard and Jan Mayen Islands', 'United States Virgin Islands', 'USSR', 'Wallis and Futuna Islands', 'Yugoslav SFR', 'Global average', 'Cocos Islands', 'Macquarie Island', 'Northern Mariana Islands and Guam', 'Comoro Islands', 'Glorioso Islands', 'Juan de Nova Island', 'Bassas da India', 'Ile Europa', 'Ile Tromelin', 'Azores', 'Cape Verde', 'Canary Islands', 'Prince Edward Islands', 'Crozet Islands', 'Amsterdam Island and Saint Paul Island', 'Kerguelen Islands', 'Heard and McDonald Islands', 'Republique du Congo', 'Clipperton Island', 'Puerto Rico and Virgin Islands of the United States', 'Guadeloupe and Martinique', 'Faeroe Islands', 'Line Islands (Kiribati)', 'Phoenix Islands (Kiribati)', 'Howland Island and Baker Island', 'Guinea Bissau', 'Ivory Coast', 'Gilbert Islands (Kiribati)', 'Northern Saint-Martin', 'East Timor', 'Oecussi Ambeno', 'Laos', 'Republic of Congo', 'Dem. Rep. Congo', 'ASEAN', 'BRIICS', 'DRC', 'EA19', 'EECCA', 'EU27_2020', 'European Union', 'G20', 'G7M', 'Lao PDR', 'OECD', 'OECDAM', 'OECDAO', 'OECDE', 'Grenade', 'Korea, Rep.', 'Egypt, Arab Rep.', 'Iran, Islamic Rep.', 'Korea (Rep.)', 'Hong Kong, China', 'Iran (Islamic Republic)', 'Cote dIvoire', 'Congo (Democratic Republic)']
not_found = []
for country in missing:
try:
print(pycountry.countries.search_fuzzy(country))
print(country)
except:
print('not found')
not_found.append(country)
print(len(missing)) #122
print(len(not_found)) #122

How retrieve location address using geopy?

I have following data frame and used a code from here
from geopy.geocoders import Nominatim
data = {'lat1': [116.51172,116.51135,116.51135,116.51627,116.47186],
'lon1': [39.92123,39.93883,39.93883,39.91034,39.91248]}
# Create DataFrame
df_test = pd.DataFrame(data)
geolocator = Nominatim(user_agent="geoapiExercises")
location = geolocator.reverse(df_test['lat']+","+ df_test['lon'], language='en')
address = location.raw['address']
df_test['suburb']= address.get('suburb', '')
df_test['postcode']= address.get('postcode', '')
df_test['road']= address.get('road', '')
I want to get 3 features from the location, however, got an error
ufunc 'add' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> dtype('<U32')
Could you help to get the necessary information?
Use pd.apply to apply the geolocator.reverse function to each row of your dataframe.
geopy gave an error that the latitude coordinates must be in the range [-90, 90], so I added latitude and longitude normalization
import pandas as pd
from geopy.geocoders import Nominatim
from math import sin, asin, fmod, pi
geolocator = Nominatim(user_agent="geoapiExercises")
data = {'lat1': [116.51172,116.51135,116.51135,116.51627,116.47186],
'lon1': [39.92123,39.93883,39.93883,39.91034,39.91248]}
# Create DataFrame
df = pd.DataFrame(data)
# latitude and longitude normalization according to formulas found at
# https://stackoverflow.com/a/31119445/50065
df['lat'] = df['lat1'].apply(lambda lat: asin(sin((lat/180.0)*pi)) * (180.0/pi))
df['lon'] = df['lon1'].apply(lambda lon: fmod(lon - 180.0, 360.0) + 180.0)
df['lat_lon'] = df['lat'].astype(str) + ',' + df['lon'].astype(str)
df['location'] = df['lat_lon'].apply(lambda lat_lon: geolocator.reverse(lat_lon, language='en'))
df['address'] = df['location'].apply(lambda loc: loc.raw['address'])
df['postcode'] = df['address'].apply(lambda addr: addr.get('postcode', 'no postcode'))
Output:
lat1
lon1
lat
lon
lat_lon
location
address
postcode
0
116.512
39.9212
63.4883
39.9212
63.488280000000024,39.92123000000001
Обозерское городское поселение, Plesetsky District, Arkhangelsk Oblast, Northwestern Federal District, 164254, Russia
{'municipality': 'Обозерское городское поселение', 'county': 'Plesetsky District', 'state': 'Arkhangelsk Oblast', 'region': 'Northwestern Federal District', 'postcode': '164254', 'country': 'Russia', 'country_code': 'ru'}
164254
1
116.511
39.9388
63.4887
39.9388
63.488650000000014,39.938829999999996
Обозерское городское поселение, Plesetsky District, Arkhangelsk Oblast, Northwestern Federal District, 164254, Russia
{'municipality': 'Обозерское городское поселение', 'county': 'Plesetsky District', 'state': 'Arkhangelsk Oblast', 'region': 'Northwestern Federal District', 'postcode': '164254', 'country': 'Russia', 'country_code': 'ru'}
164254
2
116.511
39.9388
63.4887
39.9388
63.488650000000014,39.938829999999996
Обозерское городское поселение, Plesetsky District, Arkhangelsk Oblast, Northwestern Federal District, 164254, Russia
{'municipality': 'Обозерское городское поселение', 'county': 'Plesetsky District', 'state': 'Arkhangelsk Oblast', 'region': 'Northwestern Federal District', 'postcode': '164254', 'country': 'Russia', 'country_code': 'ru'}
164254
3
116.516
39.9103
63.4837
39.9103
63.48373,39.91033999999999
Обозерское городское поселение, Plesetsky District, Arkhangelsk Oblast, Northwestern Federal District, 164254, Russia
{'municipality': 'Обозерское городское поселение', 'county': 'Plesetsky District', 'state': 'Arkhangelsk Oblast', 'region': 'Northwestern Federal District', 'postcode': '164254', 'country': 'Russia', 'country_code': 'ru'}
164254
4
116.472
39.9125
63.5281
39.9125
63.52813999999999,39.912480000000016
Обозерское городское поселение, Plesetsky District, Arkhangelsk Oblast, Northwestern Federal District, 164254, Russia
{'municipality': 'Обозерское городское поселение', 'county': 'Plesetsky District', 'state': 'Arkhangelsk Oblast', 'region': 'Northwestern Federal District', 'postcode': '164254', 'country': 'Russia', 'country_code': 'ru'}
164254

Contains three consecutive consonants

I am trying to match capital cities that contain three consecutive consonants.
This is my code:
result = [i for i in capitals if re.match("\w*[^aeiouAEIOU\W]{3}\w*", i)]
print(*result)
result = [i for i in capitals if re.match(r"\b(?=[a-z]*[aeiou]{3})[a-z]+\b", i)]
print(*result)
This is the source:
capitals = ('Kabul', 'Tirana (Tirane)', 'Algiers', 'Andorra la Vella', 'Luanda', "Saint John's", 'Buenos Aires', 'Yerevan', 'Canberra', 'Vienna', 'Baku', 'Nassau', 'Manama', 'Dhaka', 'Bridgetown', 'Minsk', 'Brussels', 'Belmopan', 'Porto Novo', 'Thimphu', 'Sucre', 'Sarajevo', 'Gaborone', 'Brasilia', 'Bandar Seri Begawan', 'Sofia', 'Ouagadougou', 'Gitega', 'Phnom Penh', 'Yaounde', 'Ottawa', 'Praia', 'Bangui', "N'Djamena", 'Santiago', 'Beijing', 'Bogota', 'Moroni', 'Kinshasa', 'Brazzaville', 'San Jose', 'Yamoussoukro', 'Zagreb', 'Havana', 'Nicosia', 'Prague', 'Copenhagen', 'Djibouti', 'Roseau', 'Santo Domingo', 'Dili', 'Quito', 'Cairo', 'San Salvador', 'London', 'Malabo', 'Asmara', 'Tallinn', 'Mbabana', 'Addis Ababa', 'Palikir', 'Suva', 'Helsinki', 'Paris', 'Libreville', 'Banjul', 'Tbilisi', 'Berlin', 'Accra', 'Athens', "Saint George's", 'Guatemala City', 'Conakry', 'Bissau', 'Georgetown', 'Port au Prince', 'Tegucigalpa', 'Budapest', 'Reykjavik', 'New Delhi', 'Jakarta', 'Tehran', 'Baghdad', 'Dublin', 'Jerusalem', 'Rome', 'Kingston', 'Tokyo', 'Amman', 'Nur-Sultan', 'Nairobi', 'Tarawa Atoll', 'Pristina', 'Kuwait City', 'Bishkek', 'Vientiane', 'Riga', 'Beirut', 'Maseru', 'Monrovia', 'Tripoli', 'Vaduz', 'Vilnius', 'Luxembourg', 'Antananarivo', 'Lilongwe', 'Kuala Lumpur', 'Male', 'Bamako', 'Valletta', 'Majuro', 'Nouakchott', 'Port Louis', 'Mexico City', 'Chisinau', 'Monaco', 'Ulaanbaatar', 'Podgorica', 'Rabat', 'Maputo', 'Nay Pyi Taw', 'Windhoek', 'No official capital', 'Kathmandu', 'Amsterdam', 'Wellington', 'Managua', 'Niamey', 'Abuja', 'Pyongyang', 'Skopje', 'Belfast', 'Oslo', 'Muscat', 'Islamabad', 'Melekeok', 'Panama City', 'Port Moresby', 'Asuncion', 'Lima', 'Manila', 'Warsaw', 'Lisbon', 'Doha', 'Bucharest', 'Moscow', 'Kigali', 'Basseterre', 'Castries', 'Kingstown', 'Apia', 'San Marino', 'Sao Tome', 'Riyadh', 'Edinburgh', 'Dakar', 'Belgrade', 'Victoria', 'Freetown', 'Singapore', 'Bratislava', 'Ljubljana', 'Honiara', 'Mogadishu', 'Pretoria, Bloemfontein, Cape Town', 'Seoul', 'Juba', 'Madrid', 'Colombo', 'Khartoum', 'Paramaribo', 'Stockholm', 'Bern', 'Damascus', 'Taipei', 'Dushanbe', 'Dodoma', 'Bangkok', 'Lome', "Nuku'alofa", 'Port of Spain', 'Tunis', 'Ankara', 'Ashgabat', 'Funafuti', 'Kampala', 'Kiev', 'Abu Dhabi', 'London', 'Washington D.C.', 'Montevideo', 'Tashkent', 'Port Vila', 'Vatican City', 'Caracas', 'Hanoi', 'Cardiff', "Sana'a", 'Lusaka', 'Harare')
This is the output:
It is missing one city this one "Port Moresby"
Minsk Thimphu Phnom Penh Kinshasa Accra Conakry Reykjavik Baghdad Kingston Bishkek Lilongwe Nouakchott Windhoek Kathmandu Amsterdam Wellington Pyongyang Castries Kingstown Edinburgh Belgrade Ljubljana Stockholm Bangkok Ashgabat Washington D.C. Tashkent
This is my expected output:
Including "Port Moresby"
Minsk Thimphu Phnom Penh Kinshasa Accra Conakry Reykjavik Baghdad Kingston Bishkek Lilongwe Nouakchott Windhoek Kathmandu Amsterdam Wellington Pyongyang Port Moresby Castries Kingstown Edinburgh Belgrade Ljubljana Stockholm Bangkok Ashgabat Washington D.C. Tashkent
Try give this a whirl:
result = [i for i in capitals if re.search("[^aeiou\W]{3}", i.lower())]
print(*result)

How can I use Python to turn country code into full name and infer the country name based on the city name on an Excel file? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm a beginner in Python.
Now I have 2 columns on my Excel file. One is country column and the other one is city column.
For the country column, most of the values are shown in country code and some of them are shown in country full name, while some values are U.S.A states code and less than 1% of them are blank.
For the city column, it clearly shows the full city name (not city code), while nearly 20% of them are blank.
How can I use Python to create a new column to show the full country name based on the country code and remain the same name if it shows the full country name in the country column, and show the U.S.A states code as the United States in the new column?
The tricky part is, in the country column, take CO as the example, Co can stand for Columbia and Colorado, I cannot be sure whether it's a country or a state at the beginning, but when I check the corresponding city name I can know it's a country or a state (ex: Longmont for Colorado, Bogota for Columbia). How can I avoid this issue in the new column and infer the full country name in the new column based on the corresponding city name?
I appreciate your help!
Explanation
Coded the task using following logic.
Process simple abbreviations such as U.S.
A country length greater than 3
Have Country and City
Find closest Country City Pair in Cities
Country Only
Find closest country match in list of countries in two letter country codes
Country length equals 3
Find country with 3 letter country codes
Country length equals 2 (could be country or state code)
Code does not exist in list of states
Must be country code, so look up country in two letter country codes
Code does not exist in list of countries
Must be state code for USA, so country is United States
Could be country or state code
Check if city with this as a state code
Check if city with this as a country code
Must be best match of these two possibilities
Note: String matching uses fuzzy matching to allow for flexibility in spelling of names
rapidfuzz library was used over fuzzywuzzy since its an order of magnitude faster
Code
import pandas as pd
from rapidfuzz import fuzz
def find_closest_country(country):
' Country with the closest name in list of countries in country code '
ratios = [fuzz.partial_ratio(country, x) for x in alpha2.values()]
rated_countries = [(info, r) for info, r in zip(alpha2.values(), ratios)]
# Best match with shortest name
return sorted(rated_countries, key = lambda x: (x[1], -len(x[0])), reverse = True)[0]
def check_city_country(city, country):
' City, Country pair closest in list of cities '
ratios = [fuzz.partial_ratio(city, x['name']) * fuzz.partial_ratio(country, x['country']) for x in cities]
rated_cities = [(info, r) for info, r in zip(cities, ratios)]
# Best match with shortest name
return sorted(rated_cities, key = lambda x: (x[1], -len(x[0])), reverse = True)[0]
def check_city_subregion(city, subregion):
' City, subresion pair closest in list of cities '
ratios = [fuzz.partial_ratio(city, x['name']) * fuzz.partial_ratio(subregion, x['subcountry']) for x in cities]
rated_cities = [(info, r) for info, r in zip(cities, ratios)]
# Best match with shortest name
return sorted(rated_cities, key = lambda x: (x[1], -len(x[0])), reverse = True)[0]
def lookup(country, city):
'''
Finds country based upon country and city
country - country name or country code
city - name of city
'''
if country.lower() == 'u.s.':
# Picks up common US acronym
country = "US"
if len(country) > 3:
# Must be country since too long for abbreviation
if city:
# Find closest city country pair in list of cities
city_info = check_city_country(city, country)
if city_info:
return city_info[0]['country']
# No city, so find closest country in list of countries (2 code abbreviations reverse lookup)
countries = find_closest_country(country)
if countries:
return countries[0]
return None
elif len(country) == 3:
# 3 letter abbreviation
country = country.upper()
return alpha3.get(country, None)
elif len(country) == 2:
# Two letter country abbreviation
country = country.upper()
if not country in states:
# Not a state code, so lookup contry from code
return alpha2.get(country, None)
if not country in alpha2:
# Not a country code, so must be state code for US
return "United States of America"
# Could be country of state code
if city:
# Have 2 digit code (could be country or state)
pos_country = alpha2[country] # possible country
pos_state = states[country] # possible state
# check closest country with this city
pos_countries = check_city_country(city, pos_country)
# If state code, country would be United States
pos_us = check_city_country(city, "United States")
if pos_countries[1] > pos_us[1]:
# Provided better match as country code
return pos_countries[0]['country']
else:
# Provided better match as state code (i.e. "United States")
return pos_us[0]['country']
else:
return alpha2[country]
else:
return None
Data
# State Codes
# https://gist.github.com/rugbyprof/76575b470b6772ce8fa0c49e23931d97
states = {"AL":"Alabama","AK":"Alaska","AZ":"Arizona","AR":"Arkansas","CA":"California","CO":"Colorado","CT":"Connecticut","DE":"Delaware","FL":"Florida","GA":"Georgia","HI":"Hawaii","ID":"Idaho","IL":"Illinois","IN":"Indiana","IA":"Iowa","KS":"Kansas","KY":"Kentucky","LA":"Louisiana","ME":"Maine","MD":"Maryland","MA":"Massachusetts","MI":"Michigan","MN":"Minnesota","MS":"Mississippi","MO":"Missouri","MT":"Montana","NE":"Nebraska","NV":"Nevada","NH":"New Hampshire","NJ":"New Jersey","NM":"New Mexico","NY":"New York","NC":"North Carolina","ND":"North Dakota","OH":"Ohio","OK":"Oklahoma","OR":"Oregon","PA":"Pennsylvania","RI":"Rhode Island","SC":"South Carolina","SD":"South Dakota","TN":"Tennessee","TX":"Texas","UT":"Utah","VT":"Vermont","VA":"Virginia","WA":"Washington","WV":"West Virginia","WI":"Wisconsin","WY":"Wyoming"}
# two letter country codes
# https://gist.github.com/carlopires/1261951/d13ca7320a6abcd4b0aa800d351a31b54cefdff4
alpha2 = {
'AD': 'Andorra',
'AE': 'United Arab Emirates',
'AF': 'Afghanistan',
'AG': 'Antigua & Barbuda',
'AI': 'Anguilla',
'AL': 'Albania',
'AM': 'Armenia',
'AN': 'Netherlands Antilles',
'AO': 'Angola',
'AQ': 'Antarctica',
'AR': 'Argentina',
'AS': 'American Samoa',
'AT': 'Austria',
'AU': 'Australia',
'AW': 'Aruba',
'AZ': 'Azerbaijan',
'BA': 'Bosnia and Herzegovina',
'BB': 'Barbados',
'BD': 'Bangladesh',
'BE': 'Belgium',
'BF': 'Burkina Faso',
'BG': 'Bulgaria',
'BH': 'Bahrain',
'BI': 'Burundi',
'BJ': 'Benin',
'BM': 'Bermuda',
'BN': 'Brunei Darussalam',
'BO': 'Bolivia',
'BR': 'Brazil',
'BS': 'Bahama',
'BT': 'Bhutan',
'BU': 'Burma (no longer exists)',
'BV': 'Bouvet Island',
'BW': 'Botswana',
'BY': 'Belarus',
'BZ': 'Belize',
'CA': 'Canada',
'CC': 'Cocos (Keeling) Islands',
'CF': 'Central African Republic',
'CG': 'Congo',
'CH': 'Switzerland',
'CI': 'Côte D\'ivoire (Ivory Coast)',
'CK': 'Cook Iislands',
'CL': 'Chile',
'CM': 'Cameroon',
'CN': 'China',
'CO': 'Colombia',
'CR': 'Costa Rica',
'CS': 'Czechoslovakia (no longer exists)',
'CU': 'Cuba',
'CV': 'Cape Verde',
'CX': 'Christmas Island',
'CY': 'Cyprus',
'CZ': 'Czech Republic',
'DD': 'German Democratic Republic (no longer exists)',
'DE': 'Germany',
'DJ': 'Djibouti',
'DK': 'Denmark',
'DM': 'Dominica',
'DO': 'Dominican Republic',
'DZ': 'Algeria',
'EC': 'Ecuador',
'EE': 'Estonia',
'EG': 'Egypt',
'EH': 'Western Sahara',
'ER': 'Eritrea',
'ES': 'Spain',
'ET': 'Ethiopia',
'FI': 'Finland',
'FJ': 'Fiji',
'FK': 'Falkland Islands (Malvinas)',
'FM': 'Micronesia',
'FO': 'Faroe Islands',
'FR': 'France',
'FX': 'France, Metropolitan',
'GA': 'Gabon',
'GB': 'United Kingdom (Great Britain)',
'GD': 'Grenada',
'GE': 'Georgia',
'GF': 'French Guiana',
'GH': 'Ghana',
'GI': 'Gibraltar',
'GL': 'Greenland',
'GM': 'Gambia',
'GN': 'Guinea',
'GP': 'Guadeloupe',
'GQ': 'Equatorial Guinea',
'GR': 'Greece',
'GS': 'South Georgia and the South Sandwich Islands',
'GT': 'Guatemala',
'GU': 'Guam',
'GW': 'Guinea-Bissau',
'GY': 'Guyana',
'HK': 'Hong Kong',
'HM': 'Heard & McDonald Islands',
'HN': 'Honduras',
'HR': 'Croatia',
'HT': 'Haiti',
'HU': 'Hungary',
'ID': 'Indonesia',
'IE': 'Ireland',
'IL': 'Israel',
'IN': 'India',
'IO': 'British Indian Ocean Territory',
'IQ': 'Iraq',
'IR': 'Islamic Republic of Iran',
'IS': 'Iceland',
'IT': 'Italy',
'JM': 'Jamaica',
'JO': 'Jordan',
'JP': 'Japan',
'KE': 'Kenya',
'KG': 'Kyrgyzstan',
'KH': 'Cambodia',
'KI': 'Kiribati',
'KM': 'Comoros',
'KN': 'St. Kitts and Nevis',
'KP': 'Korea, Democratic People\'s Republic of',
'KR': 'Korea, Republic of',
'KW': 'Kuwait',
'KY': 'Cayman Islands',
'KZ': 'Kazakhstan',
'LA': 'Lao People\'s Democratic Republic',
'LB': 'Lebanon',
'LC': 'Saint Lucia',
'LI': 'Liechtenstein',
'LK': 'Sri Lanka',
'LR': 'Liberia',
'LS': 'Lesotho',
'LT': 'Lithuania',
'LU': 'Luxembourg',
'LV': 'Latvia',
'LY': 'Libyan Arab Jamahiriya',
'MA': 'Morocco',
'MC': 'Monaco',
'MD': 'Moldova, Republic of',
'MG': 'Madagascar',
'MH': 'Marshall Islands',
'ML': 'Mali',
'MN': 'Mongolia',
'MM': 'Myanmar',
'MO': 'Macau',
'MP': 'Northern Mariana Islands',
'MQ': 'Martinique',
'MR': 'Mauritania',
'MS': 'Monserrat',
'MT': 'Malta',
'MU': 'Mauritius',
'MV': 'Maldives',
'MW': 'Malawi',
'MX': 'Mexico',
'MY': 'Malaysia',
'MZ': 'Mozambique',
'NA': 'Namibia',
'NC': 'New Caledonia',
'NE': 'Niger',
'NF': 'Norfolk Island',
'NG': 'Nigeria',
'NI': 'Nicaragua',
'NL': 'Netherlands',
'NO': 'Norway',
'NP': 'Nepal',
'NR': 'Nauru',
'NT': 'Neutral Zone (no longer exists)',
'NU': 'Niue',
'NZ': 'New Zealand',
'OM': 'Oman',
'PA': 'Panama',
'PE': 'Peru',
'PF': 'French Polynesia',
'PG': 'Papua New Guinea',
'PH': 'Philippines',
'PK': 'Pakistan',
'PL': 'Poland',
'PM': 'St. Pierre & Miquelon',
'PN': 'Pitcairn',
'PR': 'Puerto Rico',
'PT': 'Portugal',
'PW': 'Palau',
'PY': 'Paraguay',
'QA': 'Qatar',
'RE': 'Réunion',
'RO': 'Romania',
'RU': 'Russian Federation',
'RW': 'Rwanda',
'SA': 'Saudi Arabia',
'SB': 'Solomon Islands',
'SC': 'Seychelles',
'SD': 'Sudan',
'SE': 'Sweden',
'SG': 'Singapore',
'SH': 'St. Helena',
'SI': 'Slovenia',
'SJ': 'Svalbard & Jan Mayen Islands',
'SK': 'Slovakia',
'SL': 'Sierra Leone',
'SM': 'San Marino',
'SN': 'Senegal',
'SO': 'Somalia',
'SR': 'Suriname',
'ST': 'Sao Tome & Principe',
'SU': 'Union of Soviet Socialist Republics (no longer exists)',
'SV': 'El Salvador',
'SY': 'Syrian Arab Republic',
'SZ': 'Swaziland',
'TC': 'Turks & Caicos Islands',
'TD': 'Chad',
'TF': 'French Southern Territories',
'TG': 'Togo',
'TH': 'Thailand',
'TJ': 'Tajikistan',
'TK': 'Tokelau',
'TM': 'Turkmenistan',
'TN': 'Tunisia',
'TO': 'Tonga',
'TP': 'East Timor',
'TR': 'Turkey',
'TT': 'Trinidad & Tobago',
'TV': 'Tuvalu',
'TW': 'Taiwan, Province of China',
'TZ': 'Tanzania, United Republic of',
'UA': 'Ukraine',
'UG': 'Uganda',
'UM': 'United States Minor Outlying Islands',
'US': 'United States of America',
'UY': 'Uruguay',
'UZ': 'Uzbekistan',
'VA': 'Vatican City State (Holy See)',
'VC': 'St. Vincent & the Grenadines',
'VE': 'Venezuela',
'VG': 'British Virgin Islands',
'VI': 'United States Virgin Islands',
'VN': 'Viet Nam',
'VU': 'Vanuatu',
'WF': 'Wallis & Futuna Islands',
'WS': 'Samoa',
'YD': 'Democratic Yemen (no longer exists)',
'YE': 'Yemen',
'YT': 'Mayotte',
'YU': 'Yugoslavia',
'ZA': 'South Africa',
'ZM': 'Zambia',
'ZR': 'Zaire',
'ZW': 'Zimbabwe',
'ZZ': 'Unknown or unspecified country',
}
# Three letter codes
#https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3#Uses_and_applications
alpha3 = """ABW Aruba
AFG Afghanistan
AGO Angola
AIA Anguilla
ALA Åland Islands
ALB Albania
AND Andorra
ARE United Arab Emirates
ARG Argentina
ARM Armenia
ASM American Samoa
ATA Antarctica
ATF French Southern Territories
ATG Antigua and Barbuda
AUS Australia
AUT Austria
AZE Azerbaijan
BDI Burundi
BEL Belgium
BEN Benin
BES Bonaire, Sint Eustatius and Saba
BFA Burkina Faso
BGD Bangladesh
BGR Bulgaria
BHR Bahrain
BHS Bahamas
BIH Bosnia and Herzegovina
BLM Saint Barthélemy
BLR Belarus
BLZ Belize
BMU Bermuda
BOL Bolivia (Plurinational State of)
BRA Brazil
BRB Barbados
BRN Brunei Darussalam
BTN Bhutan
BVT Bouvet Island
BWA Botswana
CAF Central African Republic
CAN Canada
CCK Cocos (Keeling) Islands
CHE Switzerland
CHL Chile
CHN China
CIV Côte d'Ivoire
CMR Cameroon
COD Congo, Democratic Republic of the
COG Congo
COK Cook Islands
COL Colombia
COM Comoros
CPV Cabo Verde
CRI Costa Rica
CUB Cuba
CUW Curaçao
CXR Christmas Island
CYM Cayman Islands
CYP Cyprus
CZE Czechia
DEU Germany
DJI Djibouti
DMA Dominica
DNK Denmark
DOM Dominican Republic
DZA Algeria
ECU Ecuador
EGY Egypt
ERI Eritrea
ESH Western Sahara
ESP Spain
EST Estonia
ETH Ethiopia
FIN Finland
FJI Fiji
FLK Falkland Islands (Malvinas)
FRA France
FRO Faroe Islands
FSM Micronesia (Federated States of)
GAB Gabon
GBR United Kingdom of Great Britain and Northern Ireland
GEO Georgia
GGY Guernsey
GHA Ghana
GIB Gibraltar
GIN Guinea
GLP Guadeloupe
GMB Gambia
GNB Guinea-Bissau
GNQ Equatorial Guinea
GRC Greece
GRD Grenada
GRL Greenland
GTM Guatemala
GUF French Guiana
GUM Guam
GUY Guyana
HKG Hong Kong
HMD Heard Island and McDonald Islands
HND Honduras
HRV Croatia
HTI Haiti
HUN Hungary
IDN Indonesia
IMN Isle of Man
IND India
IOT British Indian Ocean Territory
IRL Ireland
IRN Iran (Islamic Republic of)
IRQ Iraq
ISL Iceland
ISR Israel
ITA Italy
JAM Jamaica
JEY Jersey
JOR Jordan
JPN Japan
KAZ Kazakhstan
KEN Kenya
KGZ Kyrgyzstan
KHM Cambodia
KIR Kiribati
KNA Saint Kitts and Nevis
KOR Korea, Republic of
KWT Kuwait
LAO Lao People's Democratic Republic
LBN Lebanon
LBR Liberia
LBY Libya
LCA Saint Lucia
LIE Liechtenstein
LKA Sri Lanka
LSO Lesotho
LTU Lithuania
LUX Luxembourg
LVA Latvia
MAC Macao
MAF Saint Martin (French part)
MAR Morocco
MCO Monaco
MDA Moldova, Republic of
MDG Madagascar
MDV Maldives
MEX Mexico
MHL Marshall Islands
MKD North Macedonia
MLI Mali
MLT Malta
MMR Myanmar
MNE Montenegro
MNG Mongolia
MNP Northern Mariana Islands
MOZ Mozambique
MRT Mauritania
MSR Montserrat
MTQ Martinique
MUS Mauritius
MWI Malawi
MYS Malaysia
MYT Mayotte
NAM Namibia
NCL New Caledonia
NER Niger
NFK Norfolk Island
NGA Nigeria
NIC Nicaragua
NIU Niue
NLD Netherlands
NOR Norway
NPL Nepal
NRU Nauru
NZL New Zealand
OMN Oman
PAK Pakistan
PAN Panama
PCN Pitcairn
PER Peru
PHL Philippines
PLW Palau
PNG Papua New Guinea
POL Poland
PRI Puerto Rico
PRK Korea (Democratic People's Republic of)
PRT Portugal
PRY Paraguay
PSE Palestine, State of
PYF French Polynesia
QAT Qatar
REU Réunion
ROU Romania
RUS Russian Federation
RWA Rwanda
SAU Saudi Arabia
SDN Sudan
SEN Senegal
SGP Singapore
SGS South Georgia and the South Sandwich Islands
SHN Saint Helena, Ascension and Tristan da Cunha
SJM Svalbard and Jan Mayen
SLB Solomon Islands
SLE Sierra Leone
SLV El Salvador
SMR San Marino
SOM Somalia
SPM Saint Pierre and Miquelon
SRB Serbia
SSD South Sudan
STP Sao Tome and Principe
SUR Suriname
SVK Slovakia
SVN Slovenia
SWE Sweden
SWZ Eswatini
SXM Sint Maarten (Dutch part)
SYC Seychelles
SYR Syrian Arab Republic
TCA Turks and Caicos Islands
TCD Chad
TGO Togo
THA Thailand
TJK Tajikistan
TKL Tokelau
TKM Turkmenistan
TLS Timor-Leste
TON Tonga
TTO Trinidad and Tobago
TUN Tunisia
TUR Turkey
TUV Tuvalu
TWN Taiwan, Province of China
TZA Tanzania, United Republic of
UGA Uganda
UKR Ukraine
UMI United States Minor Outlying Islands
URY Uruguay
USA United States of America
UZB Uzbekistan
VAT Holy See
VCT Saint Vincent and the Grenadines
VEN Venezuela (Bolivarian Republic of)
VGB Virgin Islands (British)
VIR Virgin Islands (U.S.)
VNM Viet Nam
VUT Vanuatu
WLF Wallis and Futuna
WSM Samoa
YEM Yemen
ZAF South Africa
ZMB Zambia
ZWE Zimbabwe"""
# Convert to dictionary
alpha3 = dict(tuple(re.split(r" {2,}", s)) for s in alpha3.split('\n'))
# List of World Cities & Country
# cities https://pkgstore.datahub.io/core/world-cities/world-cities_csv/data/6cc66692f0e82b18216a48443b6b95da/world-cities_csv.csv
# Online CSV File
import csv
import urllib.request
import io
def csv_import(url):
url_open = urllib.request.urlopen(url)
csvfile = csv.DictReader(io.StringIO(url_open.read().decode('utf-8')), delimiter=',')
return csvfile
url = 'https://pkgstore.datahub.io/core/world-cities/world-cities_csv/data/6cc66692f0e82b18216a48443b6b95da/world-cities_csv.csv'
cities = csv_import(url)
Test
Excel File (Input)
country city
u.s.
DZ
AS
co Longmont
co Bogota
AL
AL Huntsville
usa
AFG
BLR Minsk
AUS
united states
Korea seoul
Korea Pyongyang
Test Code
df = pd.read_excel('country_test.xlsx') # Load Excel File
df.fillna('', inplace=True)
# Get name of country based upon country and city
df['country_'] = df.apply(lambda row: lookup(row['country'], row['city']), axis = 1)
Resulting Dataframe
country city country_
0 u.s. United States of America
1 DZ Algeria
2 AS American Samoa
3 co Longmont United States
4 co Bogota Colombia
5 AL Albania
6 AL Huntsville United States
7 usa United States of America
8 AFG Afghanistan
9 BLR Minsk Belarus
10 AUS Australia
11 united states United States of America
12 Korea seoul South Korea
13 Korea Pyongyang North Korea
Well, You can have a {key (state) : Values (cities belonging to states)} json and use python to read the file and arrange the list to the corresponding city, state.
An advice for this approach is to create dictionaries(i.e. dic = {'CO':'Colombia',...} and dic_state = {'CO':'Colorado',...}). Then, probably have an if statement to check if the country is USA. If USA, then use dic_state. Finally, you can create a new column by using the appropriate command (this depends on the package/module that you are using)
Good luck!

convert dictionary into xls file using python openpyxl library

Below is my dictionary & I want to write the key-value pair of dictionary into excel sheet in two columns named key & hourly.
'One year reserved'= {
'Australia Central 2': 0.0097,
'East US 2': 0.00605,
'North Central US': 0.00605,
'South Africa West': 0.01016,
'UK West': 0.00685,
'France South': 0.01119,
'Korea': 0.00639,
'Canada East': 0.00685,
'US Gov Virginia': 0.00879,
'East Asia': 0.0097,
'South India': 0.01005,
'South Central US': 0.00731,
'West US': 0.00719,
'Australia East': 0.00776,
'Canada Central': 0.00674,
'Australia Southeast': 0.00776,
'Southeast Asia': 0.00776,
'Central US': 0.00731,
'West India': 0.00833,
'East US': 0.00605,
'Australia Central': 0.0097,
'UK South': 0.00685,
'Japan East': 0.00799,
'Japan West': 0.00879,
'West Europe': 0.00696,
'Brazil South': 0.00982,
'Korea Central': 0.00799,
'US Gov Texas': 0.00879,
'US Gov Arizona': 0.00879,
'Central India': 0.00833,
'North Europe': 0.00822,
'West Central US': 0.00731,
'France Central': 0.00856,
'South Africa North': 0.00811,
'West US 2': 0.00605
}
convert dictionary into xls file using python openpyxl library.
output should be like this:-
**Key** **Hourly**
Australia Central 2 0.008
East US 2 0.00605
North Central US  0.00605
Here is a solution working with python 3.6+ because it uses f-strings. enumerate is used not to have to store the row number in another reference.
from openpyxl import Workbook
data = {
'australia-central': 0.0097,
'usgov-virginia': 0.00879,
}
workbook = Workbook()
sheet = workbook.active
sheet["A1"] = "Key"
sheet["B1"] = "Hourly"
for row, (key, hourly) in enumerate(data.items(), start=2):
sheet [f"A{row}"] = key
sheet [f"B{row}"] = hourly
workbook.save("output.xlsx")
import csv
one_year_reserved = {
'australia-central': 0.0097,
'usgov-virginia': 0.00879,
'us-south-central': 0.00731,
'france-south': 0.01119,
'us-west': 0.00719,
'europe-north': 0.00822,
'asia-pacific-east': 0.0097,
'japan-east': 0.00799,
'west-india': 0.00833,
'united-kingdom-west': 0.00685,
'usgov-arizona': 0.00879,
'brazil-south': 0.00982,
'australia-east': 0.00776,
'us-west-2': 0.00605,
'asia-pacific-southeast': 0.00776,
'south-india': 0.01005,
'us-central': 0.00731,
'us-east-2': 0.00605,
'south-africa-west': 0.01016,
'canada-central': 0.00674,
'south-africa-north': 0.00811,
'canada-east': 0.00685,
'us-east': 0.00605,
'korea-south': 0.00639,
'united-kingdom-south': 0.00685,
'europe-west': 0.00696,
'japan-west': 0.00879,
'australia-southeast': 0.00776,
'us-west-central': 0.00731,
'us-north-central': 0.00605,
'central-india': 0.00833,
'korea-central': 0.00799,
'usgov-texas': 0.00879,
'france-central': 0.00856,
'australia-central-2': 0.0097
}
with open('output2.csv', 'wb') as output:
writer = csv.writer(output)
for key, value in one_year_reserved.items():
writer.writerow([key, value])
Try this code . It works fine !
# Writing to an excel
# sheet using Python
import xlwt
from xlwt import Workbook
# Workbook is created
wb = Workbook()
#dictionary
sample_data= {
'Australia Central 2': 0.0097,
'East US 2': 0.00605,
'North Central US': 0.00605,
'South Africa West': 0.01016,
'UK West': 0.00685,
'France South': 0.01119,
'Korea': 0.00639,
'Canada East': 0.00685,
'US Gov Virginia': 0.00879,
'East Asia': 0.0097,
'South India': 0.01005,
'South Central US': 0.00731,
'West US': 0.00719,
'Australia East': 0.00776,
'Canada Central': 0.00674,
'Australia Southeast': 0.00776,
'Southeast Asia': 0.00776,
'Central US': 0.00731,
'West India': 0.00833,
'East US': 0.00605,
'Australia Central': 0.0097,
'UK South': 0.00685,
'Japan East': 0.00799,
'Japan West': 0.00879,
'West Europe': 0.00696,
'Brazil South': 0.00982,
'Korea Central': 0.00799,
'US Gov Texas': 0.00879,
'US Gov Arizona': 0.00879,
'Central India': 0.00833,
'North Europe': 0.00822,
'West Central US': 0.00731,
'France Central': 0.00856,
'South Africa North': 0.00811,
'West US 2': 0.00605
}
# add_sheet is used to create sheet.
sheet1 = wb.add_sheet('Sheet 1')
#general syntax
#sheet1.write(column, row, value)
sheet1.write(0, 0, 'Key')
sheet1.write(1, 0, 'Hourly')
row = 1
#iterate the each key-value pair of dictionary & insert into sheet
for k, v in dict.items():
sheet1.write(0, row, k)
sheet1.write(1, row, v)
row = row + 1
wb.save('xlwt example.xls')

Categories