python通信达数据_Python读取通达信数据

这篇博客介绍了如何使用Python解析通达信本地日线数据,并将其转换为CSV格式,便于进一步分析。通过二进制读取和struct模块的unpack方法,详细解释了解析过程并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python读取通达信数据

一、介绍

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])

运行下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值