说明:以下程序均由《kimi助手》辅助生成,已测试正常。
1、我的申万宏源金融终端安装目录如下:
日线数据
tdx_sh_day_dir='C:/zd_swhy_gm/vipdoc/sh/lday', tdx_sz_day_dir='C:/zd_swhy_gm/vipdoc/sz/lday',
程序如下:
import struct
from pathlib import Path
class TDXDataParser:
def __init__(self, file_path):
self.file_path = Path(file_path)
if not self.file_path.exists():
raise FileNotFoundError(f"文件不存在:{self.file_path}")
self.code = self.file_path.stem # 提取文件名作为股票代码
def parse_day_file(self):
records = []
with open(self.file_path, 'rb') as fp:
while chunk := fp.read(32): # 每32字节为一天数据
date_int = struct.unpack('I', chunk[0:4])[0]
year = date_int // 10000
month = (date_int % 10000) // 100
day = date_int % 100
open_ = struct.unpack('I', chunk[4:8])[0] / 100.0
high = struct.unpack('I', chunk[8:12])[0] / 100.0
low = struct.unpack('I', chunk[12:16])[0] / 100.0
close = struct.unpack('I', chunk[16:20])[0] / 100.0
vol = struct.unpack('I', chunk[24:28])[0]
records.append({
"日期": f"{year}-{month:02d}-{day:02d}",
"开盘价": open_,
"最高价": high,
"最低价": low,
"收盘价": close,
"成交量": vol
})
return records
def print_data(self):
print(f"正在解析股票代码:{self.code}")
records = self.parse_day_file()
if not records:
print("未找到任何数据!")
else:
for record in records:
print(record)
print(f"股票代码 {self.code} 的数据解析完成!\n")
# 测试代码
if __name__ == "__main__":
file_path = 'C:/zd_swhy_gm/vipdoc/sh/lday/sh600000.day'
parser = TDXDataParser(file_path)
parser.print_data()
print("所有数据解析完成!")
运行结果已经跟通达信数据核对正确,如下: