把以下股票数据存⼊stock_data.txt
开发程序对stock_data.txt进⾏以下操作:
程序启动后执⾏效果参考:
股票查询接⼝>>:换⼿率>25
['序号', '代码', '名称', '最新价', '涨跌幅', '涨跌额', '成交量(⼿)', '成交额', '振幅',
'最⾼', '最低', '今开', '昨收', '量⽐', '换⼿率', '市盈率', '市净率']
['18', '603697', '有友⻝品', '22.73', '10.02%', '2.07', '34.93万', '7.68亿',
'8.23%', '22.73', '21.03', '21.17', '20.66', '1.4', '43.94%', '38.1', '4.66']
['23', '603956', '威派格', '22.52', '10.01%', '2.05', '18.33万', '4.01亿',
'10.60%', '22.52', '20.35', '20.35', '20.47', '2.16', '43.02%', '-', '9.82']
['36', '300748', '⾦⼒永磁', '59.7', '10.01%', '5.43', '11.02万', '6.38亿',
'6.98%', '59.7', '55.91', '56.88', '54.27', '0.9', '26.49%', '234.09',
'23.54']
['37', '300767', '震安科技', '41.13', '10.00%', '3.74', '6.22万', '2.49亿',
'10.32%', '41.13', '37.27', '37.48', '37.39', '3.86', '31.11%', '43.32',
'3.68']
['38', '603045', '福达合⾦', '32', '10.00%', '2.91', '17.06万', '5.31亿',
'9.87%', '32', '29.13', '29.13', '29.09', '1.39', '25.17%', '52.74', '4.02']
['39', '2952', '亚世光电', '58.98', '10.00%', '5.36', '4.18万', '2.41亿',
'7.42%', '58.98', '55', '55.91', '53.62', '3.04', '27.44%', '53.09', '5.51']
找到6条
股票查询接⼝>>:⻝品
['18', '603697', '有友⻝品', '22.73', '10.02%', '2.07', '34.93万', '7.68亿',
'8.23%', '22.73', '21.03', '21.17', '20.66', '1.4', '43.94%', '38.1', '4.66']
找到1条
股票查询接⼝>>:能源
['9', '2828', '⻉肯能源', '14.25', '10.04%', '1.3', '17.83万', '2.52亿',
'4.71%', '14.25', '13.64', '13.8', '12.95', '3.45', '18.03%', '-', '3.08']
找到1条
股票的数据源,去东方财富网复制,自己保存到txt文件里
这里是网址:http://quote.eastmoney.com/center/gridlist.html#hs_a_board
但我感觉你们都会有数据源的
注意要统一用逗号将数据进行隔开才能运行下面的代码,建议大家辛苦一下把数据改改
如果是其他隔开符号要修改代码
这里我提供部分股票数据:
股票代码,股票名称,当前价,涨跌额,涨跌幅,年初至今,成交量,成交额,换手率,市盈率(TTM),股息率,市值
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亿
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亿
进行股票查询的前提是股票的数据必须存在才能执行
这里要利用Python 的os工具包,判断文件是否存在,只有存在才会继续运行代码
import os
if os.path.isfile(file_name) is True and os.path.getsize(file_name) != 0:
# 检测文件是否存在且是否为空内容
funx()
elif os.path.isfile(file_name) is False:
print("没有股票数据文件或文件名错误,请检查")
elif os.path.getsize(file_name) == 0:
print("股票数据文件内容为空,请检查")
在进行查询前,得把股票数据给读取并处理了
之前说的关于数据源的隔开符号,在这里就用上了
def file_read(): # 读取并处理股票数据文件 函数
count = 0 # 股票条数计数器
data_dict = {
} # 股票数据存放点
with open(file_name, "r", encoding="Utf-8") as f:
for line in f:
count += 1 # 统计有多少个数据
file_r = line.strip().split(",")
# 源数据都是逗号隔开,使用逗号将数据分割,空格隔开直接split()
if count == 1:
fist_list = file_r
else:
data_dict[file_r[1]] = dict(zip(fist_list, file_r))
# 以股票名称 作为字典key
return data_dict, fist_list, count-1 # 这里将股票数据,头行标签,统计条数返回
file_name = "stock_data.txt" # 本地文件名
讲了两个要定义的 函数后,是时候上主菜了
咱直接上总代码,一共4个函数
import os
def file_judgment(funx):
"""
这里使用装饰器判断文件是否存在,只有存在才会运行文件
"""
def inner():
if os.path.isfile(file_name) is True and os.path.getsize(file_name) != 0:
# 检测文件是否存在且是否为空内容
funx()
elif os.path.isfile(file_name) is False:
print("没有股票数据文件或文件名错误,请检查")
elif os.path.getsize(file_name) == 0:
print("股票数据文件内容为空,请检查")
return funx()
return inner
@file_judgment
def file_read(): # 读取并处理股票数据文件 函数
count = 0 # 股票条数计数器
data_dict = {
} # 股票数据存放点
with open(file_name, "r", encoding="Utf-8") as f:
for line in f:
count += 1 # 统计有多少个数据
file_r = line.strip().split(",")
# 源数据都是逗号隔开,使用逗号将数据分割,空格隔开直接split()
if count == 1:
fist_list = file_r
else:
data_dict[file_r[1]] = dict(zip(fist_list, file_r))
# 以股票名称 作为字典key
return data_dict, fist_list, count-1 # 这里将股票数据,头行标签,统计条数返回
def data_filter(user_input, compare=None): # 查询内容判断 函数
fist_list = file_read()[1]
data_dict = file_read()[0]
match_count = 0
if compare is None:
for key in data_dict:
if user_input in key: # 这里模糊查询和具体查询都能判断
match_count += 1
print("\n", data_dict[key])
break
else:
print("\n输入查询不存在,请检查核对输入")
else:
u_k, u_v = user_input.split(compare)
if u_v.isdigit(): # 判断 比较符后是否输入为数字
u_k, u_v = u_k.strip(), float(u_v)
if u_k in fist_list[2:]: # 前两个数据没啥关系,如 股票名称 这里排除掉
for data in data_dict:
if float(data_dict[data][u_k].strip("%,亿,万")) > u_v:
# 换手率,成交额 后面都有 一个单位,这里给去掉
match_count += 1
print(f"{fist_list}\n{data_dict[data]}\n")
else:
print(f"比较数据输入错误,请重新核对输入\t{fist_list[2:]}")
print(f"\n总共匹配到 {match_count} 个数据")
else:
print("\n", "请输入正确查询内容".center(50, "="))
def data_inquire():
all_count = file_read()[2]
while True:
print(f"\n一共已存在{all_count} 条股票数据")
user_input = input("\n请输入查询内容>:")
if len(user_input) == 0:
continue
if ">" in user_input:
data_filter(user_input, ">")
elif "<" in user_input:
data_filter(user_input, "<")
elif ">=" in user_input:
data_filter(user_input, ">=")
elif "<=" in user_input:
data_filter(user_input, "<=")
else:
data_filter(user_input)
file_name = "stock_data.txt" # 本地文件名
data_inquire()