通达信day文件读取
通达信日线数据读取
https://blog.csdn.net/lh2273341049/article/details/145273996
https://gitcode.csdn.net/662b4a9cc46af926427792e8.html
优化后的代码如下:
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 5 13:28:29 2025
@author: CHENGJUN
"""
import struct
from datetime import datetime
def read_day_file(file_path):
"""
读取日线文件并解析数据。
参数:
file_path (str): 日线文件的路径。
返回值:
list: 解析后的数据列表,每个元素是一个字典,包含日期、开盘价、最高价、最低价、收盘价、成交额和成交量。
"""
# 列名列表
cols = ["date", "open", "high", "low", "close", "amount", "vol"]
# 使用with语句打开文件,确保文件在读取后正确关闭
with open(file_path, "rb") as ofile:
# 读取文件内容
buf = ofile.read()
# 使用struct.iter_unpack直接迭代解析二进制数据
items = [
{
cols[0]: datetime.strptime(str(record[0]), "%Y%m%d").strftime("%Y-%m-%d"), # 日期
cols[1]: str(record[1] / 100.0), # 开盘价(保留原始精度)
cols[2]: str(record[2] / 100.0), # 最高价(保留原始精度)
cols[3]: str(record[3] / 100.0), # 最低价(保留原始精度)
cols[4]: str(record[4] / 100.0), # 收盘价(保留原始精度)
cols[5]: str(record[5]), # 成交额
cols[6]: str(record[6] / 100.0), # 成交量
}
for record in struct.iter_unpack("IIIIIfII", buf)
]
# 返回解析后的数据列表
return items
file_path = r'D:\Program Files\PinganSecu\vipdoc\bj\lday\bj430478.day'
content = read_day_file(file_path)
df = pd.DataFrame(content)
Out[0]:
date open high low close amount vol
0 2023-02-23 20.88 21.48 18.88 19.24 168815264.0 82974.55
1 2023-02-24 19.23 23.4 18.88 21.6 112295192.0 52750.44
2 2023-02-27 20.8 21.98 20.15 21.59 75569800.0 36256.99
3 2023-02-28 21.83 24.8 21.6 23.29 91042352.0 39566.13
4 2023-03-01 22.5 22.76 21.25 21.4 69207920.0 31653.66
.. ... ... ... ... ... ... ...
403 2024-10-24 20.0 21.23 19.35 19.62 145768048.0 71753.47
404 2024-10-25 19.4 20.81 18.5 18.68 148836016.0 75489.12
405 2024-10-28 18.43 19.35 17.82 19.09 81902760.0 44060.45
406 2024-10-29 19.09 20.5 18.9 20.5 132313736.0 66834.72
407 2024-10-30 20.16 21.17 19.51 20.32 111359736.0 54632.45
[408 rows x 7 columns]
浙公网安备 33010602011771号