如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import os import struct import pandas as pd def readTdxLdayFile(fname = "C:\\TdxW_HuaTai\\vipdoc\\sh\\lday\\sh601628.day" ): dataSet = [] with open (fname, 'rb' ) as fl: buffer = fl.read() #读取数据到缓存 size = len ( buffer ) rowSize = 32 #通信达day数据,每32个字节一组数据 code = os.path.basename(fname).replace( '.day' ,'') for i in range ( 0 ,size,rowSize): #步长为32遍历buffer row = list ( struct.unpack( 'IIIIIfII' , buffer [i:i + rowSize]) ) row[ 1 ] = row[ 1 ] / 100 row[ 2 ] = row[ 2 ] / 100 row[ 3 ] = row[ 3 ] / 100 row[ 4 ] = row[ 4 ] / 100 row.pop() #移除最后无意义字段 row.insert( 0 ,code) dataSet.append(row) data = pd.DataFrame(data = dataSet,columns = [ 'code' , 'tradeDate' , 'open' , 'high' , 'low' , 'close' , 'amount' , 'vol' ]) print (data) readTdxLdayFile() |
以上这篇Python实现将通信达.day文件读取为DataFrame就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/db_guy/article/details/79067008