-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_json_hotel.py
41 lines (33 loc) · 1.23 KB
/
convert_json_hotel.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
# -*- coding: utf-8 -*-
__author__ = 'josanvel'
import json
import csv
import string
def get_fields_json():
#Abro el archivo JSON de los hoteles
with open('scrapy_hotel/scrapy_hotel/spiders/data_hotel_completo.json') as data_file:
data_json = json.load(data_file)
#Creo el archivo CSV
csv_file = open('../data/TripAdvisor/nuevo_data_hotel.csv', 'w')
#Creo la cabecera del CSV
cabecera_json = 'name,longitude,latitude,rating,NoReviews\n'
#Guardo la cabecera en el archivo CSV
csv_file.write(cabecera_json)
for element in data_json:
#Obtengo la ubicacion del Hotel en cooredenadas
location = element['location']
lng = location[0]
lat = location[1]
if not (lng is None and lat is None):
name = element['name'] #Obtengo el nombre de un hotel
name = string.replace(name, ',', '')
rating = element['rating'] #Obtengo el raiting de un hotel
reviews = element['reviews'] #Obtengo los reviews de un hotel
NoReviews = len(reviews) #Obtengo el numoero de reviews de un hotel
#Concateno la linea del CSV
line = str(name)+','+str(lng)+','+str(lat)+','+str(rating)+','+str(NoReviews)+'\n'
#Guardo la linea en el archivo CSV
if rating:
csv_file.write(line)
if __name__ == '__main__':
get_fields_json()