import struct
import datetime
import math
import time
# 根据二进制前两段拿到日期分时
def get_date_str(H1, H2):
year = math.floor(H1 / 2048) + 2004
month = math.floor(H1 % 2048 / 100)
day = H1 % 2048 % 100
hour = math.floor(H2 / 60)
minute = H2 % 60
if hour < 10:
hour = "0" + str(hour)
if minute < 10:
minute = "0" + str(minute)
return str(year) + "-" + str(month) + "-" + str(day) + " " + str(hour) + ":" + str(minute)
def stock_lc5(filepath, name):
with open(filepath, 'rb') as f:
file_object_path = 'D:/' + name + '.csv'
file_object = open(file_object_path, 'w+')
title_list = "Date,Open,High,Low,Close,Open_interest,Volume,settlement_price\n"
file_object.writelines(title_list)
while True:
li2 = f.read(32)
if not li2:
break
data2 = struct.unpack('HHffffllf', li2)
date_str = get_date_str(data2[0], data2[1])
data2_list = list(data2)
data2_list[1] = date_str
del (data2_list[0])
for dl in range(len(data2_list)):
data2_list[dl] = str(data2_list[dl])
data2_str = ",".join(data2_list)
data2_str += "\n"
file_object.writelines(data2_str)
file_object.close()
stock_lc5('28#RML8.lc5', 'RML8.lc5')
|