【python】通达信自定义数据管理器的简单应用
1.在《通达信-功能-公式系统-自定义数据管理器》中可以手工添加数据,并导出为txt文本,但手工只能添加几个,多了就费劲了。
2.在任意行情列表页上,执行《系统-数据导出》可以将几千条数据导出为txt文本,
于是决定用第2步数据来更新第1步的数据,代码如下:
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 26 27 28 29 | #encoding=utf-8 import re # read file dic={} pattern= "([0-9]{6})\s+([\u4e00-\u9fa5]+)\s+(-?[0-9]+\.[0-9]*|-?[0-9]+)\s+(-?[0-9]+\.[0-9]*|-?[0-9]+)\s+(-?[0-9]+\.[0-9]*|-?[0-9]+)\s+(-?[0-9]+\.[0-9]*|-?[0-9]+)\s+(-?[0-9]+\.[0-9]*|-?[0-9]+)\s+(-?[0-9]+\.[0-9]*|-?[0-9]+)" with open(r 'D:\new_hrzq_v6\T0002\export\沪深A股.txt' ) as infile: for line in infile: list=re.findall(pattern,line) if len(list)>0: code=list[0][0] name=list[0][7] dic[code]=name # concat lines sn=0 lines= "" for code,name in dic.items(): if str(code)[0]== "6" : sn=1 else : sn=0 line=str(sn)+ "|" +code+ "|" +name+ "|" +str( "0.000" )+ "\n" lines=lines+line # write file with open(r 'D:\new_hrzq_v6\外部数据(字符串,数值)_1.txt' , 'w' ) as outfile: outfile.write(lines) print( "ok" ) |
3.导出的自定义数据怎么用呢?答:在《行情报价 的标题栏上点鼠标右键-选择自定义数据》,就可将相应的“自定义数据”显示在行情报价列表中了。
上述代码在使用过程中发现有bug,原因是有的股票名称中有空格,还有*ST 、A 、TCL、GYQ、N、-U、-UW、-WD等文字,代码中的正则表达式不能正确匹配,需要改一下正则表达式。
后来用pandas重写,在读xls时遇到编码问题,用read_csv read_html openpyxl xlrd ,加上参数cp936 gb18030 utf-8-sig utf16LE等等统统不好使。
原因似乎是由于程序生成的xls实际是csv,且格式不规则,但是用wps显示正常?搞不明白具体原因!
最终采取逐行读文件解析,倒是简单:
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 26 27 | #encoding=utf-8 import pandas as pd from io import StringIO path1 = r "沪深A股.xls" path2 = r "外部数据(字符串,数值)_1.txt" listdf = [] f= open(path1, 'r' ) next(f) next(f) for line in f: linearr = line.strip().split( '\t' ) if (len(linearr))>7: listrow = [linearr[0].strip( "=" ).strip( "\"" ),linearr[7]] listdf.append(listrow) f.close() df=pd.DataFrame(data=listdf,columns=[ '1' , '2' ]) df.insert(loc=0, column= '0' , value= "0" ) df[ '0' ]=df[ '1' ].apply(lambda x:1 if x[0]== "6" else 0) df.insert(loc=3, column= '3' , value= "0.000" ) print(df.shape) print(df.iloc[0:3]) df.to_csv(path2,sep= '|' ,header=0,index=0) |
生成两个文件的版本
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 26 27 28 29 30 31 32 33 34 35 36 37 38 | #encoding=utf-8 import pandas as pd from io import StringIO path1 = r "沪深A股.xls" path2 = r "外部数据(字符串,数值)_1.txt" path3 = r "外部数据(字符串,数值)_2.txt" #需要第几列,从0开始数 mycolA = 8 mycolB = 9 listdf = [] f= open(path1, 'r' ) next(f) next(f) for line in f: linearr = line.strip().split( '\t' ) if (len(linearr))>mycolB: #保留代码列、相应参数列 listrow = [linearr[0].strip( "=" ).strip( "\"" ),linearr[mycolA],linearr[mycolB]] listdf.append(listrow) f.close() df=pd.DataFrame(data=listdf,columns=[ '1' , '2' , '3' ]) df.insert(loc=0, column= '0' , value= "0" ) df.insert(loc=0, column= '4' , value= "0.000" ) df[ '0' ]=df[ '1' ].apply(lambda x:1 if x[0]== "6" else 0) #选子集 df2=df[[ "0" , "1" , "2" , "4" ]] df3=df[[ "0" , "1" , "3" , "4" ]] #print(df.shape) #print(df.iloc[0:3]) #增加分隔符并保存 df2.to_csv(path2,sep= '|' ,header=0,index=0) df3.to_csv(path3,sep= '|' ,header=0,index=0) print( "ok" ) |
参考:https://www.cnblogs.com/pyhy/p/16698107.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
2022-02-16 python生成币私钥公钥
2022-02-16 python docx转pdf
2020-02-16 纯golang爬虫实战-(六)-关于cookiejar的理解 (2020-02-14 13:50)