-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertdata.py
More file actions
156 lines (111 loc) · 4.29 KB
/
Copy pathconvertdata.py
File metadata and controls
156 lines (111 loc) · 4.29 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import pandas as pd
import numpy as np
import json
import os,sys
import mysql.connector
from mysql.connector import Error
import csv
import common.common as cm
from datetime import datetime as dt
def connectMysql():
try:
mySQLconnection = mysql.connector.connect(host='13.251.123.143',port='3306',database='HotelGatewayNew',user='hotelgateway',password='SeechoitlOfwoRwObEaphOabdLVy')
sql_select_Query = "SELECT master_room_type,room_name FROM HotelGatewayNew.room_type_mapping where master_room_type is null and room_name <> '' group by room_name"
cursor = mySQLconnection .cursor()
cursor.execute(sql_select_Query)
records = cursor.fetchall()
cursor.close()
return records
except Error as e :
print ("Error while connecting to MySQL", e)
finally:
#closing database connection.
if(mySQLconnection .is_connected()):
mySQLconnection.close()
print("MySQL connection is closed")
def handle():
script_dir = os.path.split(os.path.dirname(os.path.realpath('__file__')))[0]
listarray = {"hotelTyleName":"hotelType.json","bedName":"bed.json","bedtypeName":"bedtype.json","roomTypeName":"roomType.json","viewName":"view.json"}
rel_path = "Classification/share"
listjson={}
for key in listarray.items():
bs_file_path = os.path.join(script_dir, (rel_path + "/"+ "{}").format(key[1]))
getvalue = readjson(bs_file_path)
listjson[key[0]] = getvalue
return listjson
def saveCsv():
pd =connectMysql()
listjson = handle()
df=[]
for item in pd:
savetext =handlText(item[1],listjson,item[0])
df.append(savetext)
keys = df[0].keys()
filename = str(dt.now().year)+ "-"+ str(dt.now().month) + "-" +str(dt.now().day) + "-" + str(dt.now().hour) + "-" + str(dt.now().minute) + "-"+ str(dt.now().second)
with open(filename+'people.csv', 'w') as output_file:
dict_writer = csv.DictWriter(output_file, keys)
dict_writer.writeheader()
dict_writer.writerows(df)
print (keys)
def handlText(text,listjson,room_master):
words = cm.Common.clean_text(text)
words = words.split(" ")
listHotel = {}
hotelType=""
bedType =""
bed=""
roomType=""
view=""
for word in words:
if word !="":
for item in listjson["hotelTyleName"]:
if item['name'] == word and item['name'] not in hotelType:
hotelType =hotelType + word+ " "
for item in listjson["bedName"]:
if item['name'] == word and item['name'] not in bedType:
bedType =bedType +word +" "
for item in listjson["bedtypeName"]:
if item['name'] == word and item['name'] not in bed:
bed =bed+ word + " "
for item in listjson["roomTypeName"]:
if item['name'] == word and item['name'] not in roomType:
roomType =roomType+ word+ " "
for item in listjson["viewName"] :
if item['name'] == word and item['name'] not in view:
view =view+ word + " "
if hotelType.strip() =="":
hotelType = "Standart"
else:
hotelType.strip()
if bedType.strip() =="":
bedType = "Other"
else:
bedType.strip()
if bed.strip() =="":
bed = "Other"
else:
bed.strip()
if roomType.strip() =="":
roomType = "Other"
else:
roomType.strip()
if view.strip() =="":
view = "Other"
else:
view.strip()
listHotel["hotelType"] =hotelType
listHotel["bedType"] =bedType
listHotel["bed"] =bed
listHotel["roomType"] =roomType
listHotel["view"] =view
listHotel["origin"] = text
listHotel["room_master"] = room_master
return listHotel
#for item,value in listjson.items():
# print()
#print(value[0]["name"])
def readjson(file):
with open(file,'r') as json_file:
data = json.load(json_file)
return data
saveCsv()