python读取文件命名规则_利用json文件读取命名规则,后读取潮位数据,转为指定格式存入json (基于python3.6)...

#!/usr/bin/env python

#-*- coding:utf-8 -*-

# @File:write_tide_json

# @Author:moucong

# @Datetime:2018/3/29 18:15

# @Software: PyCharm

import json

import os

import datetime

import time

import linecache

# original file path

file_path = "E:/transer/git_transer/transer/filepath/"

# json_save_path

save_path = "E:/transer/git_transer/transer/format_json/"

dataJson = json.load(open('format_file.json',encoding='UTF-8'))

wl_dat = dataJson["WL_DAT"]

wl_dat_file_path = file_path + wl_dat["filename"]

wl_dat_name = wl_dat["name"]

wl_dat_filename = wl_dat["filename"]

wl_dat_savename = wl_dat["save_name"]

wl = dataJson["WL"]

wl_file_path = file_path + wl["filename"]

wl_name = wl["name"]

wl_filename = wl["filename"]

wl_savename = wl["save_name"]

#每分钟潮位时间格式转化

def wl_time_format(JDtime,times,hour_time,minute_time):

JDtime = JDtime + minute_time * 60

dt = times + 'T'+ hour_time

time_array = datetime.datetime.strptime(dt, '%Y%m%dT%H:%M:%S')

timestamp = time.mktime(time_array.timetuple())

stime = float(JDtime) + timestamp

dt_new = time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime(float(stime)))

return dt_new

#高(低)潮位时间格式转化

def wl_high_low_time(max_time,create_time):

_time = []

for i in max_time:

_time.append(i)

Time = _time[0]+_time[1] + ':' +_time[2]+_time[3]+':00'

dt = create_time + 'T' + Time

time_array = datetime.datetime.strptime(dt, '%Y%m%dT%H:%M:%S')

timestamp = time.mktime(time_array.timetuple())

dt_new = time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime(float(timestamp)))

return dt_new

# print(_time)

#时间格式转化

def time_format(JDtime,times,start_time):

JDtime = JDtime

dt = times + 'T'+ start_time

time_array = datetime.datetime.strptime(dt, '%Y%m%dT%H:%M:%S')

timestamp = time.mktime(time_array.timetuple())

stime = float(JDtime) + timestamp

dt_new = time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime(float(stime)))

return dt_new

#写入json文件

def Write_json(save_path,data,savename):

if os.path.exists(save_path + savename + ".json") is False:

with open(save_path + savename + ".json", "w", encoding='utf-8') as f:

result = json.dump(data, f, ensure_ascii=False)

print('已写入' + ' ' + savename + ".json")

return result

else:

print('已存在' + ' ' + savename + ".json")

pass

# 整点潮位

def json_wl_dat(filepath, filename, name, savename):

# line = list(map(float,linecache.getline(filepath, 1).split()))

id = filename

id = id.split(".")

json_list = []

lineList = []

number = 1

with open(filepath, "r") as fp:

while True:

line = fp.readline()

line_data = list(map(str,linecache.getline(filepath, number).split()))

lineList.append(line_data)

number = number + 1

if not line:

break

create_time = lineList[0][0]

lineList.remove([]) #移除空list

# h_data = [Line[1:][i:i + 2] for i in range(0, len(line[1:]), 2)]

i = len(lineList[0])-11

j = 0

k = 1

start_time = '00:00:00'

while j < i:

hour = {"name": name, "stationid": "", "createAt": " ", "type": "h", "tidal_level": "",

"high_tide1": "", "high_tide1_at": "", "low_tide1": "", "low_tide1_at": "",

"high_tide2": "", "high_tide2_at": "", "low_tide2": "", "low_tide2_at": ""}

hour["tidal_level"] = lineList[0][j+1]

hour["high_tide1"] = lineList[0][-10]

hour["high_tide1_at"] = wl_high_low_time(lineList[0][-9], create_time)

hour["high_tide2"] = lineList[0][-8]

hour["high_tide2_at"] = wl_high_low_time(lineList[0][-7], create_time)

hour["low_tide1"] = lineList[0][-5]

hour["low_tide1_at"] = wl_high_low_time(lineList[0][-4], create_time)

hour["low_tide2"] = lineList[0][-3]

hour["low_tide2_at"] = wl_high_low_time(lineList[0][-2], create_time)

hour["createAt"] = time_format(3600 * j, create_time, start_time)

hour["stationid"] = id[1]

json_list.append(hour)

j = j + 1

k = k + 1

# print(hour)

Write_json(save_path, json_list, savename)

# 每分钟潮位

def json_wl(filepath, filename, name, savename):

# line = list(map(float,linecache.getline(filepath, 1).split()))

id = filename

id = id.split(".")

json_list = []

lineList = []

number = 1

with open(filepath, "r") as fp:

while True:

line = fp.readline()

line_data = list(map(str,linecache.getline(filepath, number).split()))

lineList.append(line_data)

number = number + 1

if not line:

break

create_time = lineList[0][0]

lineList.remove([]) #移除空list

# h_data = [Line[1:][i:i + 2] for i in range(0, len(line[1:]), 2)]

i = len(lineList)-2

j = 0

m = len(lineList[1])-1

n = 0

k = 1

start_time = '00:00:00'

while j < i:

while n < m:

hour = {"name": name, "stationid": "", "createAt": " ", "type": "m", "tidal_level": "",

"high_tide1": "", "high_tide1_at": "", "low_tide1": "", "low_tide1_at": "",

"high_tide2": "", "high_tide2_at": "", "low_tide2": "", "low_tide2_at": ""}

hour["tidal_level"] = lineList[j+1][n+1]

hour["high_tide1"] = lineList[25][-10]

hour["high_tide1_at"] = wl_high_low_time(lineList[25][-9], create_time)

hour["high_tide2"] = lineList[25][-8]

hour["high_tide2_at"] = wl_high_low_time(lineList[25][-7], create_time)

hour["low_tide1"] = lineList[25][-5]

hour["low_tide1_at"] = wl_high_low_time(lineList[25][-4], create_time)

hour["low_tide2"] = lineList[25][-3]

hour["low_tide2_at"] = wl_high_low_time(lineList[25][-2], create_time)

hour["createAt"] = wl_time_format(3600 * j, create_time, start_time, k)

hour["stationid"] = id[1]

json_list.append(hour)

k = k + 1

n = n + 1

n = 0

k = 1

j = j + 1

# print(hour)

Write_json(save_path, json_list, savename)

if __name__ == '__main__':

json_wl_dat(wl_dat_file_path, wl_dat_filename, wl_dat_name, wl_dat_savename)

json_wl(wl_file_path, wl_filename, wl_name, wl_savename)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值