通达信数据批量转为csv数据

本文介绍了一款用于将特定目录下的股票数据文件转换为CSV格式的Python脚本。该脚本可以批量处理文件,适用于从通达信(TDX)导出的数据格式,方便进行进一步的数据分析和处理。

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


```python
# coding: UTF-8
from struct import *
 
import os
import sys
 
 
    
def day2csv_data(dirname,fname,targetDir):
    ofile=open(dirname+os.sep+fname,'rb')
    buf=ofile.read()
    ofile.close()
     
    ifile=open(targetDir+os.sep+fname+'.csv','w')
    num=len(buf)
    no=num/32
    b=0
    e=32
    line='' 
    linename=str('date')+','+str('open')+', '+str('high')+' ,'+str('low')+', '+str('close')+' ,'+str('amout')+', '+str('vol')+' ,'+str('str07')+''+'\n'
      # print line
    ifile.write(linename)
    # for i in xrange(no):
    for i in range(int(no)):
       a=unpack('IIIIIfII',buf[b:e])
       line=str(a[0])+','+str(a[1]/100.0)+', '+str(a[2]/100.0)+' ,'+str(a[3]/100.0)+', '+str(a[4]/100.0)+' ,'+str(a[5])+', '+str(a[6])+' ,'+str(a[7])+''+'\n'
      # print line
       ifile.write(line)
       b=b+32
       e=e+32
    ifile.close()
    
 
 
# pathdir='/vipdoc/sh/lday'
pathdir='C:\\new_tdx\\vipdoc\\sh\\lday'
# targetDir='/_python_gp_tdx/data_gupiao/sh/lday'
targetDir='C:\\new_tdx\\vipdoc\\sh\\lday'
 
listfile=os.listdir(pathdir)
 
 
for f in listfile:
   
    day2csv_data(pathdir,f,targetDir)
else:
    print ('The for '+pathdir+' to '+targetDir+'  loop is over')
    
 
 
 

测试可用.
原文:https://blog.youkuaiyun.com/liuyukuan/article/details/53560278?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-7.control&dist_request_id=&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-7.control
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值