编程语言
首页 > 编程语言> > python之处理股票数据的.day文件

python之处理股票数据的.day文件

作者:互联网

读取.day文件保存在Excel中:

# "D:\StudyFiles\ClassFile\股票证券实验报告\证券日交易行情和文本基本信息\SHday\sh000001.day"
import os
import time
from struct import unpack
import pandas as pd


# 获取day文件然后转换为正常文本
def read_data(fname, code):
    ''' 读取通达信day数据 '''
    data = []
    with open(fname, 'rb') as f:
        buf = f.read()
    num = len(buf)  # 总长度
    no = num / 32  # 分块长度
    b = 0  # 开始指针
    e = 32  # 每一个小块的长度

    for i in range(int(no)):
        a = unpack('IIIIIfII', buf[b:e])
        data_time = toDataTime(a[0])
        openPrice = a[1] / 100.0
        high = a[2] / 100.0
        low = a[3] / 100.0
        close = a[4] / 100.0
        amount = a[5] / 100.0
        vol = a[6] / 100.0
        # 把数据添加到列表
        # [股票代码,开盘价,最高价,最低价,收盘价,成交额,成交量]
        data.append([code, data_time, openPrice, high, low, close, amount, vol])
        b += 32
        e += 32
    return data


# 将数据转换为时间
def toDataTime(longTime):
    # val = val*100
    longTime = longTime / 1000  # float 时间戳格式(1019948462.2750368)
    t = time.localtime(longTime)
    # 

标签:文件,code,suf,python,tm,fname,time,data,day
来源: https://www.cnblogs.com/0099-ymsml/p/16165909.html