-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_data.py
83 lines (65 loc) · 2.74 KB
/
load_data.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
import csv
#! /usr/bin/python
# To change this template, choose Tools | Templates
# and open the template in the editor.
__author__="udevel"
__date__ ="$22 mai 2014 16:03:10$"
# Set the Django env
from django.core.management import setup_environ
from boombike import settings
setup_environ(settings)
# Imports
import os
import sys
import traceback
from csv import reader
from info.models import City, FRANCE_COUNTRY_NAME, SPAIN_COUNTRY_NAME, PORTUGAL_COUNTRY_NAME
CITIES_FILE_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')
FRENCH_CITIES_FILE_PATH = os.path.join(CITIES_FILE_PATH, 'villes_france.csv')
SPANISH_CITIES_FILE_PATH = os.path.join(CITIES_FILE_PATH, 'municipios-calles.txt')
PORTUGUESE_CITIES_FILE_PATH = os.path.join(CITIES_FILE_PATH, 'todos_cp.txt')
def load_french_cities():
with open(FRENCH_CITIES_FILE_PATH, 'r') as cities_file:
csv_reader = reader(cities_file, delimiter=',', quoting=csv.QUOTE_ALL)
for row in csv_reader:
try:
zip_code = row[7].split('-')[0]
city = City(name=unicode(row[4], 'utf-8'),
zip_code=zip_code,
country=FRANCE_COUNTRY_NAME)
city.save()
except:
traceback.print_exc()
sys.stderr.write(unicode(row[4], 'utf-8') + u' could not be saved\n')
def load_spanish_cities():
with open(SPANISH_CITIES_FILE_PATH, 'r') as cities_file:
csv_reader = reader(cities_file, delimiter=';', quoting=csv.QUOTE_NONE)
for row in csv_reader:
try:
if row[2] == "false":
zip_code = row[3]
city = City(name=unicode(row[4], 'utf-8'),
zip_code=zip_code,
country=SPAIN_COUNTRY_NAME)
city.save()
except:
traceback.print_exc()
sys.stderr.write(unicode(row[4], 'utf-8') + u' could not be saved\n')
def load_portugues_ciies():
with open(PORTUGUESE_CITIES_FILE_PATH, 'r') as cities_file:
csv_reader = reader(cities_file, delimiter=';', quoting=csv.QUOTE_NONE)
for row in csv_reader:
try:
zip_code = row[14]
print zip_code, unicode(row[16], 'utf-8')
city = City(name=unicode(row[16], 'utf-8'),
zip_code=zip_code,
country=PORTUGAL_COUNTRY_NAME)
city.save()
except:
# traceback.print_exc()
sys.stderr.write(unicode(row[4], 'utf-8') + u' could not be saved\n')
if __name__ == "__main__":
# load_french_cities()
# load_spanish_cities()
load_portugues_ciies()