Python读取通达信本地数据 您所在的位置:网站首页 通达信外部数据导入 Python读取通达信本地数据

Python读取通达信本地数据

2024-04-22 17:55| 来源: 网络整理| 查看: 265

一、介绍

python获取股票数据的方法很多,其中 Tushare 财经数据接口包很好用,当然,也可以通过通达信本地的数据获取,这样更为方便。 日线数据存在这路径下 D:\通达信\vipdoc\sh\lday(我的通达信安装目录是D盘)

接着我们需要的就是解析这些数据,在分别存为 csv 格式的数据就行了,这样我们可以方便的用 pandas 或其他方法读取和分析。 通达信的日线数据格式如下:

每32个字节为一天数据 每4个字节为一个字段,每个字段内低字节在前 00 ~ 03 字节:年月日, 整型 04 ~ 07 字节:开盘价*100, 整型 08 ~ 11 字节:最高价*100, 整型 12 ~ 15 字节:最低价*100, 整型 16 ~ 19 字节:收盘价*100, 整型 20 ~ 23 字节:成交额(元),float型 24 ~ 27 字节:成交量(股),整型 28 ~ 31 字节:(保留)

打开一个 .day 的文件,发现是乱码,以二进制格式存储,那么我们只需按照上面存的字节数解析下就可以了。 先读取一天的数据

>>> f = open('D:/通达信/vipdoc/sh/lday/sh000001.day', 'rb') >>> f.read(32) b'\xa2\xde2\x01\x14\x9b\x03\x00\x0f\x9d\x03\x00\x8d\x91\x03\x00\xef\x93\x03\x00\xcb\xbc\x14Q\x9a\xfb|\x02-\x01Z\x02'

这应该就是一天的数据了,我们对这个数据进行解析,这里需要用到 struct 模块中的 unpack 方法

>>> import struct >>> f = open('D:/通达信/vipdoc/sh/lday/sh000001.day', 'rb') >>> li = f.read(32) >>> data = struct.unpack('lllllfll', li) >>> data (20111010, 236308, 236815, 233869, 234479, 39926411264.0, 41745306, 39452973) # 分别为日期,开盘,最高,最低,收盘,成交额,成交量,保留值

unpack用法: 前一个参数是格式,'lllllfii' 就是一个浮点数格式(f,这里对应日线数据中的成交额是float格式)和其他整形格式(i,这里对应日线数据中的其他数据是 int 格式) 那么剩下的问题不大了

二、完整代码

在 sh 目录下新建了个 pythondata 文件夹,注意文件路径分隔符是 /

import struct import datetime def stock_csv(filepath, name): data = [] with open(filepath, 'rb') as f: file_object_path = 'D:/通达信/vipdoc/sh/pythondata/' + name +'.csv' file_object = open(file_object_path, 'w+') while True: stock_date = f.read(4) stock_open = f.read(4) stock_high = f.read(4) stock_low= f.read(4) stock_close = f.read(4) stock_amount = f.read(4) stock_vol = f.read(4) stock_reservation = f.read(4) # date,open,high,low,close,amount,vol,reservation if not stock_date: break stock_date = struct.unpack("l", stock_date) # 4字节 如20091229 stock_open = struct.unpack("l", stock_open) #开盘价*100 stock_high = struct.unpack("l", stock_high) #最高价*100 stock_low= struct.unpack("l", stock_low) #最低价*100 stock_close = struct.unpack("l", stock_close) #收盘价*100 stock_amount = struct.unpack("f", stock_amount) #成交额 stock_vol = struct.unpack("l", stock_vol) #成交量 stock_reservation = struct.unpack("l", stock_reservation) #保留值 date_format = datetime.datetime.strptime(str(stock_date[0]),'%Y%M%d') #格式化日期 list= date_format.strftime('%Y-%M-%d')+","+str(stock_open[0]/100)+","+str(stock_high[0]/100.0)+","+str(stock_low[0]/100.0)+","+str(stock_close[0]/100.0)+","+str(stock_vol[0])+"\r\n" file_object.writelines(list) file_object.close() stock_csv('D:/通达信/vipdoc/sh/lday/sh000001.day', '1')

运行下,打开 1.CSV 文件

OK

三、批量解析 import os import struct import datetime def stock_csv(filepath, name): data = [] with open(filepath, 'rb') as f: file_object_path = 'D:/通达信/vipdoc/sh/pythondata/' + name +'.csv' file_object = open(file_object_path, 'w+') while True: stock_date = f.read(4) stock_open = f.read(4) stock_high = f.read(4) stock_low= f.read(4) stock_close = f.read(4) stock_amount = f.read(4) stock_vol = f.read(4) stock_reservation = f.read(4) # date,open,high,low,close,amount,vol,reservation if not stock_date: break stock_date = struct.unpack("l", stock_date) # 4字节 如20091229 stock_open = struct.unpack("l", stock_open) #开盘价*100 stock_high = struct.unpack("l", stock_high) #最高价*100 stock_low= struct.unpack("l", stock_low) #最低价*100 stock_close = struct.unpack("l", stock_close) #收盘价*100 stock_amount = struct.unpack("f", stock_amount) #成交额 stock_vol = struct.unpack("l", stock_vol) #成交量 stock_reservation = struct.unpack("l", stock_reservation) #保留值 date_format = datetime.datetime.strptime(str(stock_date[0]),'%Y%M%d') #格式化日期 list= date_format.strftime('%Y-%M-%d')+","+str(stock_open[0]/100)+","+str(stock_high[0]/100.0)+","+str(stock_low[0]/100.0)+","+str(stock_close[0]/100.0)+","+str(stock_vol[0])+"\r\n" file_object.writelines(list) file_object.close() path = 'D:/通达信/vipdoc/sh/lday/' listfile = os.listdir('D:/通达信/vipdoc/sh/lday/') for i in listfile: stock_csv(path+i, i[:-4])

运行下

完美



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有