开发程序对stock_data.txt进行以下操作:
思路提示:加载⽂件内容到内存,转成dict or list结构,然后对dict or list 进行查询等操作。这样以后就不用每查一次就要打开一次文件了,效率会高。
程序启动后执行效果参考:
股票查询接⼝口>>:换⼿手率>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条
股票查询接⼝口>>:最新价<5
数据源已经帮你准备好了了
stock_data.txt
['序号', '代码', '名称', '最新价', '涨跌幅', '涨跌额', '成交量量(⼿手)', '成交额', '振幅',
'最⾼高', '最低', '今开', '昨收', '量量⽐比', '换⼿手率', '市盈率', '市净率']
['2', '2676', '顺威股份', '3.69', '10.15%', '0.34', '15.23万', '5516万',
'9.55%', '3.69', '3.37', '3.37', '3.35', '1.16', '2.11%', '-', '2.58']
['3', '601619', '嘉泽新能', '4.91', '10.09%', '0.45', '16.55万', '8006万',
'8.52%', '4.91', '4.53', '4.54', '4.46', '1.82', '3.28%', '52.26', '3.64']
找到2条
股票查询接⼝口>>:⻝⾷食品
['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条
股票查询接⼝口>>:科技
['12', '2866', '传艺科技', '13.81', '10.04%', '1.26', '13.59万', '1.83亿',
'9.72%', '13.81', '12.59', '12.61', '12.55', '2.63', '16.86%', '33.37',
'3.43']
['19', '300777', '中简科技', '24.92', '10.02%', '2.27', '5952', '1483万',
'0.00%', '24.92', '24.92', '24.92', '22.65', '3.45', '1.49%', '102.24',
'11.49']
['21', '300245', '天玑科技', '11.53', '10.02%', '1.05', '26.86万', '3.05亿',
'9.64%', '11.53', '10.52', '10.52', '10.48', '1.06', '10.35%', '127.47',
'2.57']
['26', '300391', '康跃科技', '7.8', '10.01%', '0.71', '3.9万', '3027万',
'10.01%', '7.8', '7.09', '7.09', '7.09', '0.75', '1.94%', '27.35', '1.89']
['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']
['40', '603327', '福蓉科技', '21.56', '10.00%', '1.96', '3586', '773.1万',
'0.00%', '21.56', '21.56', '21.56', '19.6', '2.81', '0.70%', '31.97', '8.05']
找到6条
import operator
class stocks():
def readfile(self, file):
"""读取文件,将股票数据转化成列表格式"""
stocks_data = []
with open(file, 'r', encoding='utf-8')as f:
for line in f.readlines():
line = line.strip('\n') # 去掉换行
line_list = line.strip(',').split(',') # 按输入字符分割,将其转化为数组
stocks_data.append(line_list)
return stocks_data
def inquirys(self, stocks_data):
"""
处理查询请求,对请求进行字符串拆分
command :查询命令
condition :判断条件
parameter : 条件参数
"""
requests = input("===================\n请输入查询指令>>:").strip()
# print(stocks_data[0]) # .replace(',', " ")
if '>' in requests:
index = requests.index('>')
command = requests[0:index]
condition = '>'
parameter = requests[index+1:]
self.processData(stocks_data, command, parameter, condition)
elif "<" in requests:
index = requests.index('<')
command = requests[0:index]
condition = '<'
parameter = requests[index + 1:]
self.processData(stocks_data, command, parameter, condition)
else:
command = requests
self.mohuchaxun(stocks_data, command)
def processData(self,stocks_data, command, parameter, condition):
"""处理数据,解决数据单位不同,或者数据有分号情况"""
new_stocks_data = []
for stocks_info in stocks_data:
stocks_data = []
for info in stocks_info:
info = info.replace('%', '')
if '亿' in info:
info = str(round(float(info[0:-1]) * 10000, 3)) + '万'
stocks_data.append(info)
new_stocks_data.append(stocks_data)
# 传递打印
self.print_data(new_stocks_data, command, parameter, condition)
def print_data(self, new_stocks_data, command, parameter, condition):
"""
打印显示数据
# operator模块中函数,可以将将运算符映射到函数 参考https://docs.python.org/zh-cn/3/library/operator.html
"""
print(new_stocks_data[0])
count = 0
operator_dict = {"<": operator.lt, ">": operator.gt, "=": operator. eq} # 通过字典方式将传进来的字符串转换为运算符
operator_func = operator_dict[condition]
res = new_stocks_data[0].index(command)
for stock_info in new_stocks_data[1:]:
if "万" in stock_info[res]:
stock_info[res] = stock_info[res][:-1]
if operator_func(float(stock_info[res]), float(parameter)):
count += 1 # 计数
print(stock_info)
print(f"找到{count}条数据!")
def mohuchaxun(self,stocks_data, command):
"""模糊查询"""
list_data = []
print(stocks_data[0])
for info in stocks_data[1:]:
for x in info:
if x.find(command) != -1:
list_data.append(info)
for i in list_data:
print(i)
print(f"找到{len(list_data)}条数据!")
def func(self, file):
# 运行函数
try:
stocks_data = self.readfile(file)
while True:
self.inquirys(stocks_data)
except:
print("数据读取错误!")
if __name__ == '__main__':
obj = stocks()
obj.func(file="../res/stock_data.txt")
python代码有一个特点就是优雅和简洁