股票数据分析&处理
开发程序对stock_data.txt进⾏以下操作:
程序启动后,给⽤户提供查询接⼝,允许⽤户重复查股票⾏情信息(⽤到循环)
允许⽤户通过模糊查询股票名,⽐如输⼊“股份”, 就把所有股票名称中包含“股份”的信息打印出来
允许按股票价格、涨跌幅、换⼿率这⼏列来筛选信息,⽐如输⼊“当前价>12”则把价格⼤于12的股票
都打印,输⼊“当前价<12“,则把当前价格⼩于12的股票都打印,不⽤判断等于。
思路提示:加载⽂件内容到内存,转成dict or list结构,然后对dict or list 进⾏查询等操作。 这样以后
就不⽤每查⼀次就要打开⼀次⽂件了,效率会⾼。
代码实现
tail=open("gupiao","r",encoding="utf-8")zd={}for i in tail:i=i.strip().split(",") #去除字符串中换行,以,为分隔符分割为列表zd[i[1]]=i #将列表赋值给字典print(zd["股票名称"]) #输出字典的值while True:#模糊查找name=input("请输入>>:")if name not in zd:a=str(name.find(">")) #规定name为str类型在name中查找>b=str(name.find("<"))c = str(name.find("="))if a!="-1":#a!=-1输入字符中含有大于号a1=name.split(">")#以大于号分割,得到含有名称和数字的表a2=str(zd["股票名称"]) #定义字典中股票名称的头部表为字符串类型方面在内容中查找a3=a2.find(a1[0]) # 在头表字符串中查找输入的内容的名称a2=zd["股票名称"]#将字符串型转回列表型if a3==-1: #-1为没有字符需要重新输入print("找不到")else: #不是-1说明找到内容zd.pop("股票名称")#将头部表删除防止循环是浮点类型中有字符串a4=a2.index(f"{a1[0]}")#在a2列表中查找名称返回在列表中的索引a7=zd.values()for i in a7: #将字典的values循环a5=float(i[a4]) #a5为查找到的索引值对应的数据a6=float(a1[1]) #a6为输入的数字数据if a5>a6: #如果a5>a6满足>条件输出内容print(i)#输出每次遍历表内容zd["股票名称"] = ['股票代码', '股票名称', '当前价', '涨跌额', '涨跌幅', '年初至今', '成交量', '成交额', '换手率', '市盈率(TTM)','股息率', '市值']#将头表再次添加防止查找头表时出错elif b!="-1":b1=name.split("<")b2 = str(zd["股票名称"])b3 = b2.find(b1[0])b2 = zd["股票名称"]if b3 == -1:print("找不到")else:zd.pop("股票名称")b4 = b2.index(f"{b1[0]}")b7 = zd.values()for i in b7:b5 = float(i[b4])b6 = float(b1[1])if b5 < b6:print(i)zd["股票名称"] = ['股票代码', '股票名称', '当前价', '涨跌额', '涨跌幅', '年初至今', '成交量', '成交额', '换手率', '市盈率(TTM)', '股息率', '市值']elif c!="-1":c1=name.split("=")c2 = str(zd["股票名称"])c3 = c2.find(c1[0])c2 = zd["股票名称"]if c3 == -1:print("找不到")else:zd.pop("股票名称")c4 = c2.index(f"{c1[0]}")c7 = zd.values()for i in c7:c5 = float(i[c4])c6 = float(c1[1])if c5 == c6:print(i)zd["股票名称"] = ['股票代码', '股票名称', '当前价', '涨跌额', '涨跌幅', '年初至今', '成交量', '成交额', '换手率', '市盈率(TTM)', '股息率', '市值']for i,v in zd.items():#模糊查找模块jfc=str(i) #将name换为str字符型便于查找f=jfc.find(name) #在字符串中查找输入的内容if f!=-1: #如果找到则输出否则重新输入print(v)print("请重新输入")else:print(zd[name]) #如果输入与字典name一直则直接输出对应的valuse的值
gupaio文件文件
股票代码,股票名称,当前价,涨跌额,涨跌幅,年初至今,成交量,成交额,换手率,市盈率(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亿
如果觉得《股票查询系统 模糊查询 判断数值 字典 列表 字符串的运用python》对你有帮助,请点赞、收藏,并留下你的观点哦!