分享

【python】通达信自定义数据管理器的简单应用

 Jeremy_苍 2024-06-09 发布于安徽

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  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多