-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindCountryForCities.py
131 lines (119 loc) · 3.88 KB
/
FindCountryForCities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 19 16:16:01 2014
Get the country name of each city using database of countries
in countries_list.txt
Write the countries to a database
@author: dvats
"""
# find the country for each city
import pandas as pd
import numpy as np
import MySQLdb as db
file_name = "./data/FilteredTravelData.csv"
country_file_name = "./data/countries_list.txt"
usecols = ["Region", "title"]
main_data = pd.read_csv(file_name, usecols=usecols).fillna(value='')
region = np.array(main_data["Region"])
list_countries = open(country_file_name, 'r').readlines()[0].split('\r')
list_countries = np.array ([ll.lstrip().rstrip().replace('&', 'and').lower() for ll in list_countries])
list_countries_new = np.array(['_'.join(ll.split()) for ll in list_countries])
countries = ['']*len(region)
for ind in range(len(region)):
rr = region[ind].lower()
val = np.zeros(len(list_countries))
for i, c in enumerate(list_countries):
val[i] = c in rr
if sum(val) == 1.0:
ind_c = np.where(val == True)[0][0]
countries[ind] = list_countries[ind_c]
if sum(val) == 0.0:
val1 = np.zeros(len(list_countries_new))
for i, c in enumerate(list_countries_new):
val1[i] = c in rr
if sum(val1) == 1.0:
ind_c = np.where(val1 == True)[0][0]
countries[ind] = ' '.join(list_countries[ind_c].split('_'))
if 'scotland' in rr:
countries[ind] = 'united kingdom'
if 'united states' in rr:
countries[ind] = 'united states'
if 'united kingdom' in rr:
countries[ind] = 'united kingdom'
if 'united_states' in rr:
countries[ind] = 'united states'
if 'united_kingdom' in rr:
countries[ind] = 'united kingdom'
if 'saudi arabia' in rr:
countries[ind] = 'saudi arabia'
if 'new zealand' in rr:
countries[ind] = 'new zealand'
if 'new_zealand' in rr:
countries[ind] = 'new zealand'
if 'south korea' in rr:
countries[ind] = 'south korea'
if 'south_korea' in rr:
countries[ind] = 'south korea'
if 'north_korea' in rr:
countries[ind] = 'north korea'
if 'north carolina' in rr:
countries[ind] = 'united states'
if 'florida' in rr:
countries[ind] = 'united states'
if 'new york' in rr:
countries[ind] = 'united states'
if 'toronto' in rr:
countries[ind] = 'canada'
if 'lehigh' in rr:
countries[ind] = 'united states'
if 'Italy' in rr:
countries[ind] = 'italy'
if 'greek' in rr:
countries[ind] = 'greece'
if 'greece' in rr:
countries[ind] = 'greece'
if 'new south wales' in rr:
countries[ind] = 'australia'
if 'detroit' in rr:
countries[ind] = 'united states'
if 'north west england' in rr:
countries[ind] = 'united kingdom'
if 'ohio' in rr:
countries[ind] = 'united states'
if 'jammu' in rr:
countries[ind] = 'india'
if 'nigeria' in rr:
countries[ind] = 'nigeria'
if 'romania' in rr:
countries[ind] = 'romania'
if 'NorwayNorway' in rr:
countries[ind] = 'norway'
if 'Nova Scotia' in rr:
countries[ind] = 'canada'
if 'east england' in rr:
countries[ind] = 'united kingdom'
if 'east of england' in rr:
countries[ind] = 'united kingdom'
if 'ontario' in rr:
countries[ind] = 'canada'
if 'auckland' in rr:
countries[ind] = 'australia'
con = db.connect('localhost', 'root', '', 'guide_data')
cr = con.cursor()
cr.execute("drop table if exists CountryList;")
# create table columns
tmp = '''
create table CountryList (
id int(7),
country text,
primary key (id)
);
'''
cr.execute(tmp)
for i in range(len(countries)):
tmp_c = "INSERT INTO CountryList (id, country) VALUES (%s, %s)"
tmp_v = [i+1, countries[i]]
cr.execute(tmp_c, tuple(tmp_v))
con.commit()
cr.close()
con.close()