在我们的项目中,我使用字典添加国家/地区关联以定位我们的优惠。
以下是代码段(country_items是sqlite3中关系中的行的字符串,如" bd,be,bn"):
country_dict = {'BD': 'Bangladesh', 'BE': 'Belgium', 'BF': 'Burkina-faso', 'BG': 'Bulgaria', 'BB': 'Barbados', 'BM': 'BermudaIs.', 'BN': 'Brunei', 'BO': 'Bolivia', 'BH': 'Bahrain', 'BI': 'Burundi', 'BJ': 'Benin', 'JM': 'Jamaica', 'BW': 'Botswana', 'BR': 'Brazil', 'BS': 'Bahamas', 'BY': 'Belarus', 'BZ': 'Belize', 'RU': 'Russia', 'TM': 'Turkmenistan', 'TJ': 'Tajikstan', 'RO': 'Romania', 'GU': 'Guam', 'GT': 'Guatemala', 'GR': 'Greece', 'JP': 'Japan', 'GY': 'Guyana', 'GF': 'French Guiana', 'GE': 'Georgia', 'GD': 'Grenada', 'GB': 'United Kiongdom', 'GA': 'Gabon', 'GN': 'Guinea', 'GM': 'Gambia', 'GI': 'Gibraltar', 'GH': 'Ghana', 'OM': 'Oman', 'TN': 'Tunisia', 'JO': 'Jordan', 'HT': 'Haiti', 'HU': 'Hungary', 'HK': 'Hongkong', 'HN': 'Honduras', 'VE': 'Venezuela', 'PR': 'PuertoRico', 'PT': 'Portugal', 'MD': 'Moldova, Republic of', 'IQ': 'Iraq', 'PA': 'Panama', 'PF': 'French Polynesia', 'PG': 'Papua New Cuinea', 'PE': 'Peru', 'PK': 'Pakistan', 'PH': 'Philippines', 'PL': 'Poland', 'ZM': 'Zambia', 'EE': 'Estonia', 'EG': 'Egypt', 'ZA': 'South Africa', 'EC': 'Ecuador', 'IT': 'Italy', 'VN': 'Vietnam', 'SB': 'Solomon Is', 'ET': 'Ethiopia', 'SO': 'Somali', 'ZW': 'Zimbabwe', 'ES': 'Spain', 'on': 'Ascens', 'MG': 'Madagascar', 'MA': 'Morocco', 'MC': 'Monaco', 'UZ': 'Uzbekistan', 'MM': 'Burma', 'ML': 'Mali', 'MO': 'Macao', 'MN': 'Mongolia', 'US': 'United States of America', 'MU': 'Mauritius', 'MT': 'Malta', 'MW': 'Malawi', 'MV': 'Maldives', 'PY': 'Paraguay', 'MS': 'Montserrat Is', 'UG': 'Uganda', 'MY': 'Malaysia', 'MX': 'Mexico', 'IL': 'Israel', 'FR': 'France', 'FI': 'Finland', 'FJ': 'Fiji', 'NI': 'Nicaragua', 'NL': 'Netherlands', 'NO': 'Norway', 'NA': 'Namibia', 'NE': 'Niger', 'NG': 'Nigeria', 'NZ': 'NewZealand', 'ZR': 'Zaire', 'NP': 'Nepal', 'NR': 'Nauru', 'CK': 'Cook Is.', 'CH': 'Switzerland', 'CO': 'Colombia', 'CN': 'China', 'CM': 'Cameroon', 'CL': 'Chile', 'CA': 'Canada', 'CG': 'Congo', 'CF': 'Central African Republic', 'CZ': 'Czech Republic', 'CY': 'Cyprus', 'CR': 'Costa Rica', 'CU': 'Cuba', 'SZ': 'Swaziland', 'SY': 'Syria', 'KG': 'Kyrgyzstan', 'KE': 'Kenya', 'SR': 'Suriname', 'KH': 'Kampuchea (Cambodia )', 'SV': 'EISalvador', 'ST': 'Sao Tome and Principe', 'SK': 'Slovakia', 'KR': 'Korea', 'SI': 'Slovenia', 'KP': 'North Korea', 'KW': 'Kuwait', 'SN': 'Senegal', 'SM': 'San Marino', 'SL': 'Sierra Leone', 'SC': 'Seychelles', 'KZ': 'Kazakstan', 'SA': 'Saudi Arabia', 'SG': 'Singapore', 'SE': 'Sweden', 'SD': 'Sudan', 'DO': 'Dominica Rep.', 'DJ': 'Djibouti', 'DK': 'Denmark', 'DE': 'Germany', 'YE': 'Yemen', 'DZ': 'Algeria', 'UY': 'Uruguay', 'YU': 'Yugoslavia', 'LB': 'Lebanon', 'LC': 'St.Lucia', 'LA': 'Laos', 'TW': 'Taiwan', 'TT': 'Trinidad and Tobago', 'TR': 'Turkey', 'LK': 'Sri Lanka', 'LI': 'Liechtenstein', 'LV': 'Latvia', 'TO': 'Tonga', 'LT': 'Lithuania', 'LU': 'Luxembourg', 'LR': 'Liberia', 'LS': 'Lesotho', 'TH': 'Thailand', 'TG': 'Togo', 'TD': 'Chad', 'LY': 'Libya', 'VC': 'St.Vincent', 'AE': 'United Arab Emirates', 'AD': 'Andorra', 'AG': 'Antigua and Barbuda', 'AF': 'Afghanistan', 'AI': 'Anguilla', 'IS': 'Iceland', 'IR': 'Iran', 'AM': 'Armenia', 'AL': 'Albania', 'AO': 'Angola', 'AR': 'Argentina', 'AU': 'Australia', 'AT': 'Austria', 'IN': 'India', 'TZ': 'Tanzania', 'AZ': 'Azerbaijan', 'IE': 'Ireland', 'ID': 'Indonesia', 'UA': 'Ukraine', 'st': 'IvoryCo', 'QA': 'Qatar', 'MZ': 'Mozambique'}
try:
...
country_items = row[5].upper().split(",")
for country_item in country_items:
if country_item in country_dict:
req_server = client.request("Offer",
"addTargetCountry",
id=req_server.data['Offer']['id'],
country_code=country_dict[country_item])
else:
print "Item " + country_item + " is absent in our dictionary."
在添加优惠的那一刻,我收到了一个错误:
Traceback (most recent call last):
File "hasoff.py", line 108, in create_offers_for_advertiser
req_server = client.request("Offer", "addTargetCountry", id=req_server.data['Offer']['id'], country_code=country_dict[country_item])
TypeError: string indices must be integers
为什么呢?怎么了?当我们使用字符串作为与字典中的值关联的键时,为什么字符串索引必须是整数?