提示:转到python专栏,观看更多内容!
点我直达–>python专栏
开发程序对stock_data.txt进⾏以下操作:
个人练手写的,和题目有点出入,只是提供了一下自己的思路。使用dataframe实现。
我的实现效果:
复制到自己新建的stock_data.csv文件
股票代码,股票名称,当前价,涨跌额,涨跌幅,年初至今,成交量,成交额,换手率,市盈率(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亿
# @Time : 2021/12/1 10:13
# @Author : 南黎
# @FileName: Day 4 股票查询程序数据.py
import pandas as pd
import numpy as np
#读入数据为dataframe格式 stock_data.csv 就是数据源,因为作业只交py所以没有放上
stock_data = pd.read_csv("stock_data.csv", encoding="utf-8",header=0)#编码格式utf-8,第一行作为索引
#输入搜索类别模块
while True:
print("#" * 10+"输入搜索类别模块"+"#" * 10)
searchKEY = input("请输入搜索关键字:股票代码,股票名称,当前价,涨跌额,涨跌幅,年初至今,成交量,成交额,换手率,市盈率(TTM),股息率,市值"
"\n或是数字代码(比如说输入1表示股票代码):1股票代码,2股票名称,3当前价,4涨跌额,5涨跌幅,6年初至今,7成交量,8成交额,9换手率,10市盈率(TTM),11股息率,12市值\n请输入:")
if searchKEY == "1":
searchKEY = "股票代码"
break
elif searchKEY == "2":
searchKEY = "股票名称"
break
elif searchKEY == "3":
searchKEY = "当前价"
break
elif searchKEY == "4":
searchKEY = "涨跌额"
break
elif searchKEY == "5":
searchKEY = "涨跌幅"
break
elif searchKEY == "6":
searchKEY = "年初至今"
break
elif searchKEY == "7":
searchKEY = "成交量"
break
elif searchKEY == "8":
searchKEY = "成交额"
break
elif searchKEY == "9":
searchKEY = "换手率"
break
elif searchKEY == "10":
searchKEY = "市盈率(TTM)"
break
elif searchKEY == "11":
searchKEY = "股息率"
break
elif searchKEY == "12":
searchKEY = "市值"
break
else:
print("输入错误!重新输入")
print("你选择了" + searchKEY)
print("#" * 10 + "输入搜索要求开始" + "#" * 10)
searchRequest=input("输入搜索要求,比如说>25表示大于25的数据,输入=25表示包含25的数据,亏损会显示为-1\n请输入:")
print("你选择了" + searchRequest)
# 过滤数据模块
print("#" * 10 + "过滤数据开始" + "#" * 10)
stock_data_new=stock_data.copy() #新建一个数据副本存储过滤数据异常字符后的数据
ignoreStrs=["万", "亿" , "+", "%"]#用于过滤掉特殊的字符串字符,过滤掉后再转float比较,注意这里有个市盈率(TTM)的 “亏损” 要特殊处理
col_names=["股票代码","股票名称","当前价","涨跌额","涨跌幅","年初至今","成交量","成交额","换手率","市盈率(TTM)","股息率","市值"]
for col_name in col_names:
for i in range(stock_data.shape[0]):
data=str(stock_data[col_name][i])
if data=="亏损":#亏损设置为-1
stock_data_new.loc[i, [col_name]] = -1
continue
for ignoreStr in ignoreStrs:
data=data.strip(ignoreStr)
# stock_data_new[col_name][i]=data #如果这么写会链式编程报错SettingWithCopyWarning ,要改为下面的语句
stock_data_new.loc[i,[col_name]]=data
# print(stock_data_new)#测试打印过滤异常字符后的数据
# 查询数据模块
print("#" * 10 + "查询数据开始" + "#" * 10)
panduan = searchRequest[0] # 判断字符
message = searchRequest[1:] # 搜索信息
# print(panduan) #测试打印判断字符
# print(message)#测试打印搜索信息
if searchKEY=="股票代码" or searchKEY=="股票名称":#只有这两个是字符串不能用于比较大小转化为float
search = stock_data_new[searchKEY].str.contains(message) # 根据查询条件获取被查询的对象
print("检测到所选字段无法比较数据,默认切换到包含搜索")
print(stock_data_new[search]) # 输入要查询的内容
else:
if panduan == ">":
search = stock_data_new[searchKEY].apply(float)> float(message) # 根据查询条件获取被查询的对象
print(stock_data_new[search]) # 输入要查询的内容
elif panduan == "=":
search = stock_data_new[searchKEY].str.contains(message) # 根据查询条件获取被查询的对象
print(stock_data_new[search]) # 输入要查询的内容
elif panduan == "<":
search = stock_data_new[searchKEY].apply(float) < float(message) # 根据查询条件获取被查询的对象
print(stock_data_new[search]) # 输入要查询的内容
大家喜欢的话,给个,点个关注!继续跟大家分享敲代码过程中遇到的问题!