[Python] 纯文本查看 复制代码
import os
import sys
import time
import winreg
import struct
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Software\HexinSoft")
hexinpath, type = winreg.QueryValueEx(key, "FilePath") # 同花顺安装路径
installdate, type = winreg.QueryValueEx(key, "InstallDate") # 安装日期
print("同花顺安装路径:", hexinpath, "安装日期:", installdate)
winreg.CloseKey(key)
daylines = [] # 股市列表
textpath = r"F:\Documents\StockData" #文本文件存储目录
shaseday = r"history\shase\day" # 沪市日线路径
sznseday = r"history\sznse\day" # 深市日线路径
stbday = r"history\stb\day" # 北京日线路径
daylines.append(shaseday)
daylines.append(sznseday)
daylines.append(stbday)
# print(daylines)
if len(sys.argv)>1:
today = str(sys.argv[1]) # 运行参数 (样式为YYYYMMDD,如20220301)
else:
loctime=time.localtime(time.time())
today = time.strftime("%Y%m%d",loctime) # 无运行参数,则默认为当天(样式为YYYYMMDD,如20220301)
txtfile = os.path.join(textpath, today+".txt") # 输出文本文件名
if os.path.exists(txtfile):
os.remove(txtfile) # 若文本文件存在则先删除
# print(txtfile, today)
for market in daylines:
dayfilepath = os.path.join(hexinpath, market) # 日线文件存储完整路径
daylinefiles = os.listdir(dayfilepath) # 日线文件名列表
# print(txtfilepath, hexinpath)
if len(daylinefiles) > 0:
# print(len(daylinefiles))
for dfn in daylinefiles:
if ".day" in dfn:
stkday = os.path.join(dayfilepath, dfn)
(shotname, extension) = os.path.splitext(dfn)
# txtfile = os.path.join(txtfilepath, shotname+".txt")
# print(stkday,txtfile)
binfile = open(stkday, 'rb')
binfile.seek(6)
recnums = struct.unpack('i', binfile.read(4))[0] # 记录数
recbgn = struct.unpack('h', binfile.read(2))[0] # 记录开始地址
reclen = struct.unpack('h', binfile.read(2))[0] # 记录长度
tf = open(txtfile,"a+")
for i in range(recnums):
binfile.seek(recbgn + reclen * i)
stkdate = struct.unpack('I', binfile.read(4))[0] # 日期
if stkdate == int(today):
stkopen = (struct.unpack('I', binfile.read(4))[0] & 0xFFFFFF) / 1000 # 开盘价
stkhigh = (struct.unpack('I', binfile.read(4))[0] & 0xFFFFFF) / 1000 # 最高价
stklow = (struct.unpack('I', binfile.read(4))[0] & 0xFFFFFF) / 1000 # 最低价
stkclose = (struct.unpack('I', binfile.read(4))[0] & 0xFFFFFF) / 1000 # 收盘价
amount = (struct.unpack('I', binfile.read(4))[0] & 0xFFFFFFF) * 10 # 成交金额
money = struct.unpack('I', binfile.read(4))[0] # 成交量
# print(shotname, stkopen, stkhigh, stklow, stkclose, amount, money)
print(shotname, stkopen, stkhigh, stklow, stkclose, amount, money, file=tf) # 写入本文文件
binfile.close()
tf.close()
print("文件输出完毕!")