遍曆win10檔案夾並解析json檔案,按照json格式存入mongo資料庫(基於python 3.6)

來源:互聯網
上載者:User

標籤:for   ati   insert   ==   設計   pen   efault   obj   enum   

import re
import json
from pymongo import 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()

def load_Mongo(Address, port, Process_data, Name):
client = MongoClient(Address, port) #資料地址+連接埠號碼
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(‘已存放,‘ + ‘ObjectID:‘ + ‘‘ + id)
def loadFont(address, port, Json_File):
n = 0
Date = []
Info = []
ADV= []
LAT= []
LON= []
TIME= []
WIND= []
PR= []
STAT = []
Data_Header = []
json_File = Json_File #json檔案地址
with open(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_Num in range(len(Data_header)):
head_Find = re.findall(r‘\n‘, Data_header[head_Num], re.I)
if head_Find: #尋找\n並刪除,然後加入json檔案
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 count in range(len(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]
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 = []
def traverse(json_Dir):
fs = os.listdir(json_Dir)
for file in 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(‘檔案: %s‘ % tmp_path)
else:
# print(‘檔案夾:%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總檔案夾地址
traverse(json_Path) #遍曆檔案夾,擷取檔案地址
for json_Filenum in range(len(List)):
loadFont(mongo_address, mongo_port, List[json_Filenum]) #載入mongo

遍曆win10檔案夾並解析json檔案,按照json格式存入mongo資料庫(基於python 3.6)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.