Import re
Import JSON
From PymongoImport Mongoclient
Import OS
# def Write_json (data):
# Open_json = open ("1.json", "a")
# Write_json = json.dumps (data)
# Open_json.write (Write_json)
# Open_json.close ()
DefLoad_mongo (Address, Port client = mongoclient (address< Span style= "COLOR: #cc7832", port) #数据地址 + port number
db = Client.portal #数据库
collection = Db.typhoons #数据表
# Collection.find ({Name})
Collection.insert (process_data)
for i in collection.find ({" ename ": Name}):
ID = str (I[ ' _id '])
print ( ' stored, ' + "ObjectID: ' +
Loadfont (address, Port, Json_file):
0
Date = []
Info = []
adv= []
lat= []
lon= []
Time= []
wind= []
pr= []
STAT = []
Data_header = []
#json文件地址
WithOpen (Json_file,encoding=' Utf-8 ')As F:
While True:
lines = F.readline ()
n = n+1
If not lines:
Break
Pass
Colon = Re.findall (R ': ', lines, Re. I)
If Colon:#处理第一行数据
Date = Re.split (R ":", lines)#分割冒号
Date = date[1].replace ('\ r', '). Replace ('\ n', '). Replace (' \ t', '). Replace (", ")
Date.append (date)
# Print (Date)
Else:
if n = =2:
info = lines
info = Info.replace (‘\ r‘,"). Replace (‘\ n‘,"). Replace (‘\ t‘,‘‘)
Info.append (Info)
Else
if n = =3:
Data_header = Re.split (R "", lines)
While‘‘In Data_header:#移除空格
Data_header.remove (‘‘)
For Head_numInchRangeLen (Data_header)):
Head_find = Re.findall (R ' \ n ', Data_header[head_num], Re. I)
If Head_find:#查找 \ n and delete, then add the JSON file
Data_header[head_num] = Data_header[head_num].replace (‘\ r‘,"). Replace ('\ n', ' ). replace (' \t', ')
# Json_list.setdefault (Data_header[head_num]) #增加键值对
Data_header.append (Data_header[head_num])
Else:
Head_data = Data_header[head_num]
# Json_list.setdefault (head_data) #增加键值对
Data_header.append (Head_data)
# Write_json (json_list) #写入json
# Print (data_header)
Else
If n >3:
all = Re.split (R "", lines)
While‘‘In all:
All.remove (‘‘)
Adv.append (all[0])
Lat.append (all[1])
Lon.append (all[2])
Time.append (all[3])
Wind.append (all[4])
Pr.append (all[5])
Stat = []
For CountInchRangeLen (All)):
If Count >5:
Stat.append (All[count])
# Print (STAT)
Else
Continue
Stat = "". Join (str (i) for I in stat) #合并数据
Stat = stat.replace ('\ r', '). Replace ('\ n', '). Replace (' \ t', ')
Stat.append (STAT)
# Print (STAT)
#设计字典
Addit_all = []
Json_list = {' Ename ':‘‘,' Is_current ':' NULL ',' EndDate ':' NULL ',' Name ':' NULL ',' SN ':' NULL ',' Land ': [],' StartDate ':‘‘,
' track ': []}
json_list[' StartDate '] = date[0]
json_list[' ename '] = info[0]
i = len (ADV)
J = 0
while J < i:
# json_list [' startdate '] = Time[j]
Span style= "COLOR: #808080" > Addit = {}
Addit [ ' forecast '] = ' null '
if stat[j] = = ' Tropical STORM ':
addit[ ' type '] = ' TS '
elif stat[j] = = ' tropical DEPRESSION ':
Addit[ ' type '] = TD "
else:
addit[' type '] = Stat[j]
addit[' Speed '] = Wind[j]
addit[' movedirection '] =' NULL '
# addit[' ADV '] = adv[j]
addit[' Longitude '] = Lon[j]
addit[' latitude '] = lat[j]
Addit [' RADIUS10 '] =' NULL '
addit[' time '] = Time[j]
addit[' RADIUS7 '] =' NULL '
Addit [' pressure '] = Pr[j]
Addit [' power '] = ' null '
addit[' des '] = '
addit[' movespeed ' = ' null '
Addit_all.append (addit) #将多组数据加入数组
j = j + 1
json_list[' track '] = Addit_all
# Write_json (json_list)
Print (json_list)
Load_mongo (address, Port, Json_list, info[0])
List = []
DefTraverse (Json_dir):
FS = Os.listdir (Json_dir)
For fileIn FS:
Tmp_path = Os.path.join (json_dir, file)
If not Os.path.isdir (Tmp_path):
File_path = Tmp_path
List.append (File_path)
# print (' File:%s '% Tmp_path)
Else
# Print (' folder:%s '% Tmp_path)
Traverse (Tmp_path)
Return List
If __name__== "__main__":
mongo_address = ' 127.0.0.1 ' #数据库地址
Mongo_port = 27017 #数据库端口
Json_path = "E:\Typhoon_data \data ' # JSON total folder address
traverse (json_path) #遍历文件夹, get the file address
for json_filenum in range (len (List)):
Loadfont (Mongo_address #载入mongo
Traverse the Win10 folder and parse the JSON file into the MONGO database in JSON format (based on Python 3.6)