小试牛刀 python股票查询程序
程序员文章站
2022-07-08 15:48:29
股票查询程序一、具体需求程序启动后,给用户提供查询接口,允许用户重复查股票行情信息(用到循环)允许按股票价格、涨跌幅、换手率等来筛选信息加载文件内容到内存,以字典的形式保存,这样就不用每次都打开文件,效率高。二、具体步骤详解1.所用到的数据信息文件代码如下(示例):股票代码,股票名称,当前价,涨跌额,涨跌幅,年初至今,成交量,成交额,换⼿率,市盈率(TTM),股息率,市值SH601778,N晶科,6.29,+1.92,+43.94%,+43.94%,259.66万,1625.5....
股票查询程序
一、具体需求
- 程序启动后,给用户提供查询接口,允许用户重复查股票行情信息(用到循环)
- 允许按股票价格、涨跌幅、换手率等来筛选信息
- 加载文件内容到内存,以字典的形式保存,这样就不用每次都打开文件,效率高。
二、具体步骤详解
1.所用到的数据信息文件
代码如下(示例):
股票代码,股票名称,当前价,涨跌额,涨跌幅,年初至今,成交量,成交额,换⼿率,市盈率(TTM),股息率,市值
SH601778,N晶科,6.29,+1.92,+43.94%,+43.94%,259.66万,1625.52万,0.44%,22.32,-,173.95亿
SH688566,吉⻉贝尔,52.66,+6.96,+15.23%,+122.29%,1626.58万,8.09亿,42.29%,89.34,-,98.44亿
SH688268,华特⽓气体,88.80,+11.72,+15.20%,+102.51%,622.60万,5.13亿,22.87%,150.47,-,106.56亿
SH600734,实达集团,2.60,+0.24,+10.17%,-61.71%,1340.27万,3391.14万,2.58%,亏损,0.00%,16.18亿
SH900957,凌云B股,0.36,+0.033,+10.09%,-35.25%,119.15万,42.10万,0.65%,44.65,0.00%,1.26亿
SZ000584,哈⼯工智能,6.01,+0.55,+10.07%,-4.15%,2610.86万,1.53亿,4.36%,199.33,0.26%,36.86亿
SH600599,熊猫⾦金金控,6.78,+0.62,+10.06%,-35.55%,599.64万,3900.23万,3.61%,亏损,0.00%,11.25亿
SH600520,⽂文⼀一科技,8.21,+0.75,+10.05%,-24.05%,552.34万,4464.69万,3.49%,亏损,0.00%,13.01亿
SH603682,锦和商业,11.73,+1.07,+10.04%,+48.29%,2746.63万,3.15亿,29.06%,29.62,-,55.42亿
SZ300831,派瑞股份,12.27,+1.12,+10.04%,+208.29%,25.38万,311.41万,0.32%,60.59,-,39.26亿
SH900939,汇丽B,0.504,+0.046,+10.04%,-23.52%,123.86万,61.86万,1.41%,52.80,0.00%,9147.60万
SH600880,博瑞传播,4.39,+0.40,+10.03%,+10.03%,1117.75万,4816.57万,1.02%,95.87,0.50%,48.00亿
SZ000609,中迪投资,7.68,+0.70,+10.03%,+39.64%,2663.08万,2.00亿,9.12%,58.84,0.00%,22.98亿
SZ300328,宜安科技,15.80,+1.44,+10.03%,+7.19%,432.45万,6832.68万,0.95%,64.80,0.51%,72.72亿
SZ002988,豪美新材,17.33,+1.58,+10.03%,+58.41%,3.50万,60.68万,0.06%,24.42,-,40.34亿
SZ000615,京汉股份,4.61,+0.42,+10.02%,+4.06%,518.09万,2388.41万,0.69%,亏损,0.00%,36.06亿
SZ300443,金雷股份,18.33,+1.67,+10.02%,+32.74%,987.25万,1.75亿,6.19%,18.78,1.08%,43.64亿
SH600506,⾹香梨梨股份,11.42,+1.04,+10.02%,+11.96%,1039.48万,1.18亿,7.04%,亏损,0.00%,16.87亿
SZ300505,川⾦金金诺,15.61,+1.42,+10.01%,-11.26%,493.54万,7555.40万,6.03%,26.70,0.85%,20.40亿
SZ300830,⾦金金现代,14.95,+1.36,+10.01%,+239.77%,63.66万,951.76万,0.74%,70.76,0.00%,64.30亿
SH603630,拉芳家化,17.26,+1.57,+10.01%,+27.85%,949.49万,1.60亿,4.19%,209.44,0.83%,39.13亿
SZ002655,共达电声,11.43,+1.04,+10.01%,-14.64%,1683.51万,1.88亿,4.68%,166.69,0.00%,41.15亿
SZ300460,惠伦晶体,16.59,+1.51,+10.01%,+0.97%,430.28万,6974.52万,2.56%,亏损,0.00%,27.92亿
SH603929,亚翔集成,20.33,+1.85,+10.01%,+33.40%,1373.66万,2.76亿,6.44%,84.29,1.25%,43.38亿
SH603392,万泰⽣生物,35.97,+3.27,+10.00%,+311.09%,6.92万,248.91万,0.16%,65.24,-,155.97亿
SZ000788,北北⼤大医药,6.82,+0.62,+10.00%,+2.40%,832.44万,5583.87万,1.40%,89.69,0.32%,40.65亿
SH601609,⾦金金⽥田铜业,10.89,+0.99,+10.00%,+66.26%,4027.64万,4.28亿,16.64%,32.38,0.32%,158.66亿
SZ000403,双林林⽣生物,52.48,+4.77,+10.00%,+62.48%,171.75万,9013.54万,0.63%,88.80,0.23%,143.05亿
SZ300832,新产业,72.80,+6.62,+10.00%,+131.92%,10.73万,780.85万,0.26%,40.20,0.62%,299.64亿
SZ002985,北北摩⾼高科,92.58,+8.42,+10.00%,+310.92%,2422.97万,21.55亿,64.54%,60.48,1.08%,139.02亿
SH603348,⽂文灿股份,18.06,+1.64,+9.99%,-25.15%,357.86万,6328.87万,4.39%,89.39,0.89%,41.83亿
SH603900,莱绅通灵,8.92,+0.81,+9.99%,-5.61%,1364.10万,1.15亿,4.01%,43.78,5.06%,30.37亿
SH603042,华脉科技,17.85,+1.62,+9.98%,+17.05%,491.44万,8705.99万,4.86%,128.50,0.31%,24.28亿
SZ300260,新莱应材,19.83,+1.80,+9.98%,+68.48%,1800.45万,3.48亿,14.16%,65.15,0.73%,40.04亿
SZ000557,⻄西部创业,3.42,+0.31,+9.97%,-0.87%,2751.17万,9408.89万,1.89%,79.39,0.00%,49.88亿
SZ300716,国⽴立科技,9.94,+0.90,+9.96%,-4.24%,142.25万,1413.92万,1.50%,亏损,0.29%,15.91亿
SZ002449,国星光电,11.92,+1.08,+9.96%,-6.80%,5185.52万,6.03亿,8.53%,21.51,2.73%,73.72亿
SZ002397,梦洁股份,8.50,+0.77,+9.96%,+73.47%,810.06万,6885.51万,1.67%,110.68,2.35%,64.95亿
SZ002662,京威股份,4.09,+0.37,+9.95%,+54.92%,8588.12万,3.40亿,5.89%,亏损,0.00%,61.35亿
SZ002297,博云新材,6.30,+0.57,+9.95%,-1.87%,2852.27万,1.74亿,6.07%,亏损,0.00%,29.69亿
SZ300351,永贵电器器,8.62,+0.78,+9.95%,-2.16%,1443.26万,1.23亿,5.89%,亏损,0.00%,33.07亿
SH600834,申通地铁,7.63,+0.69,+9.94%,+5.68%,223.20万,1703.02万,0.47%,74.74,0.56%,36.42亿
SZ000616,海海航投资,3.10,+0.28,+9.93%,+27.57%,5002.95万,1.51亿,3.50%,133.28,0.00%,44.34亿
SH600662,强⽣生控股,5.54,+0.50,+9.92%,+22.03%,254.89万,1412.07万,0.24%,亏损,0.72%,58.36亿
SZ300223,北北京君正,107.20,+8.92,+9.08%,+23.01%,930.81万,9.86亿,7.34%,314.71,0.00%,216.28亿
SZ002978,安宁股份,35.46,+2.95,+9.07%,+29.09%,1181.96万,4.11亿,29.10%,25.11,-,142.31亿
SZ002084,海海鸥住⼯工,7.09,+0.54,+8.24%,+9.24%,1647.83万,1.16亿,3.36%,33.83,0.00%,35.60亿
SH688012,中微公司,229.00,+17.30,+8.17%,+147.84%,448.32万,10.05亿,8.72%,607.21,-,1224.83亿
SZ002049,紫光国微,74.04,+5.54,+8.09%,+45.63%,4459.03万,32.57亿,7.35%,85.03,0.10%,449.29亿
SH600329,中新药业,14.83,+1.10,+8.01%,+7.00%,1159.16万,1.68亿,2.05%,18.90,2.16%,114.61亿
SZ002204,⼤大连重⼯工,5.71,+0.41,+7.74%,+77.88%,1.73亿,9.35亿,8.97%,213.83,0.55%,110.28亿
SH688088,虹软科技,84.16,+5.96,+7.62%,+78.12%,290.59万,2.43亿,7.22%,148.96,-,341.69亿
SZ300398,⻜飞凯材料料,21.33,+1.50,+7.56%,+42.68%,2676.82万,5.65亿,6.21%,47.18,0.00%,110.41亿
SZ300298,三诺⽣生物,21.93,+1.53,+7.50%,+48.45%,832.56万,1.80亿,1.89%,46.66,1.44%,123.97亿
SH603236,移远通信,235.40,+16.09,+7.34%,+61.34%,96.10万,2.22亿,4.31%,151.64,0.22%,209.93亿
SZ002074,国轩⾼高科,26.93,+1.79,+7.12%,+85.09%,3628.58万,9.54亿,3.60%,亏损,0.00%,304.25亿
SZ000710,⻉贝瑞基因,51.27,+3.37,+7.04%,+40.08%,411.57万,2.09亿,2.91%,63.69,0.00%,181.81亿
SZ300624,万兴科技,95.86,+6.28,+7.01%,+51.92%,374.27万,3.54亿,7.52%,84.07,0.38%,77.91亿
SZ300002,神州泰岳,6.42,+0.41,+6.82%,+96.93%,9118.57万,5.66亿,5.59%,亏损,0.00%,126.10亿
SH603012,创⼒力力集团,7.30,+0.46,+6.73%,-18.16%,2648.28万,1.92亿,4.16%,15.57,1.01%,46.47亿
SZ300144,宋城演艺,31.70,+1.98,+6.66%,+2.56%,980.92万,3.08亿,0.82%,45.22,0.66%,460.77亿
SZ300724,捷佳伟创,67.58,+4.18,+6.59%,+78.36%,223.02万,1.48亿,1.31%,57.75,0.28%,216.70亿
SZ200706,瓦轴B,2.27,+0.14,+6.57%,-21.99%,25.88万,57.83万,0.16%,亏损,0.00%,9.18亿
SH603693,江苏新能,12.10,+0.74,+6.51%,+18.86%,2377.38万,2.87亿,12.32%,19.47,1.32%,74.78亿
SZ200468,宁通信B,1.82,+0.11,+6.43%,-26.32%,53.22万,96.40万,0.53%,亏损,0.00%,3.89亿
SH600426,华鲁恒升,18.11,+1.09,+6.40%,-8.86%,1369.76万,2.46亿,0.85%,13.16,2.05%,294.59亿
SH900953,凯⻢马B,0.334,+0.020,+6.37%,-30.13%,186.93万,63.29万,0.78%,236.41,0.00%,2.14亿
SH600703,三安光电,25.15,+1.50,+6.34%,+36.98%,1.08亿,26.73亿,2.65%,95.77,0.42%,1025.72亿
SZ300118,东⽅方⽇日升,12.75,+0.74,+6.16%,-7.94%,3686.23万,4.68亿,5.29%,13.57,1.63%,114.92亿
SZ300418,昆仑万维,22.25,+1.29,+6.15%,+32.84%,3641.22万,8.00亿,4.03%,18.07,0.12%,257.36亿
SH900929,锦旅B股,1.214,+0.070,+6.12%,-25.52%,36.05万,43.02万,0.55%,19.81,0.00%,1.61亿
SZ300496,中科创达,60.79,+3.49,+6.09%,+34.99%,1062.96万,6.40亿,3.75%,96.33,0.22%,244.69亿
SH600818,中路路股份,12.36,+0.71,+6.09%,+3.69%,921.26万,1.15亿,3.87%,亏损,0.00%,39.73亿
SZ300644,南京聚隆隆,35.19,+2.01,+6.06%,+18.64%,872.06万,3.12亿,21.10%,79.62,0.55%,22.53亿
SZ300729,乐歌股份,24.18,+1.38,+6.05%,+3.50%,204.82万,4889.15万,8.14%,31.62,1.31%,21.09亿
SZ300767,震安科技,108.58,+6.16,+6.01%,+79.44%,146.41万,1.57亿,3.19%,92.38,0.21%,86.86亿
SH603179,DR新泉股,19.30,+1.08,+5.93%,+35.50%,147.38万,2833.24万,0.65%,35.11,2.25%,56.76亿
SH603626,科森科技,11.27,+0.63,+5.92%,+8.47%,466.11万,5187.87万,1.01%,亏损,0.00%,53.30亿
SZ300019,硅宝科技,9.50,+0.53,+5.91%,-6.13%,423.46万,3943.10万,1.55%,24.58,1.65%,31.40亿
SZ002756,永兴材料料,16.18,+0.90,+5.89%,-7.34%,247.83万,3910.01万,1.34%,18.38,2.98%,58.21亿
SZ002498,汉缆股份,6.11,+0.34,+5.89%,+102.99%,2.01亿,12.04亿,6.05%,43.98,0.62%,202.93亿
SZ002980,华盛昌,62.41,+3.46,+5.87%,+319.14%,969.65万,5.99亿,29.09%,51.97,-,83.21亿
SZ002371,北北⽅方华创,176.88,+9.80,+5.87%,+101.00%,1006.35万,17.45亿,2.20%,276.58,0.04%,875.76亿
SZ300139,晓程科技,8.71,+0.48,+5.83%,+0.23%,4423.87万,3.82亿,20.11%,亏损,0.00%,23.84亿
SZ000636,风华⾼高科,24.51,+1.33,+5.74%,+64.61%,5605.61万,13.61亿,6.26%,71.71,0.00%,219.42亿
SZ000564,供销⼤大集,4.06,+0.22,+5.73%,+69.87%,2.98亿,11.89亿,14.86%,亏损,0.00%,244.52亿
SZ002612,朗姿股份,7.84,+0.42,+5.66%,-19.34%,540.71万,4191.22万,2.31%,1224.17,4.23%,34.69亿
SH600176,中国巨⽯石,9.39,+0.50,+5.62%,-13.85%,3256.19万,3.02亿,0.93%,16.97,2.14%,328.87亿
SH603313,梦百合,24.10,+1.28,+5.61%,+14.16%,356.10万,8446.14万,1.05%,21.72,0.00%,82.23亿
SZ300279,和晶科技,5.49,+0.29,+5.58%,-8.50%,2328.20万,1.31亿,5.33%,亏损,0.00%,24.65亿
2.代码展示
简陋版
import re
f = open("sock_data.txt", "r", encoding='UTF-8')
dict = {}
title = f.readline().strip().split(',')
print(title)
for line in f.readlines():
line = line.strip().split(',')
dict[line[1]] = line
while True:
data = input()
if data == 'exit':
exit("byebye")
if '>' not in data and '<' not in data:
print("输入格式错误,请重新输入!")
continue
a = re.split('>|<', data) #正则,按>或者<来分隔,获得分隔后的list
key = data[len(a[0])] #将符号拎出来
index = title.index(a[0])
if a[0] not in title:
print("不存在的查询项!")
continue
print(title)
for i in dict.values():
if key == '>':
#使用正则将字符串中的所有浮点数和整数提取出来,再将string转化成float类型即可
if float(re.findall(r'\-*\d+(?:\.\d+)?', i[index])[0]) > float(re.findall(r'\-*\d+(?:\.\d+)?', a[1])[0]):
print(i)
elif key == '<':
if float(re.findall(r'\-*\d+(?:\.\d+)?', i[index])[0]) < float(re.findall(r'\-*\d+(?:\.\d+)?', a[1])[0]):
print(i)
3 运行结果
总结
- 在使用变量名时尽量避免使用与函数同名的变量名,会发生’ str’ object is not callable的错误;
-在字符串中匹配所有浮点数和整数的正则是:r’-*\d+(?:.\d+)?
本文地址:https://blog.csdn.net/weixin_43407213/article/details/110926432