import re
f = open("stock.txt", encoding="utf-8")
e = f.read()
e = e.replace("\t", ",") #将缓存e中所有制表符替换为,
a = "".join(e).split("\n") #将缓存e中按\n分隔生成列表a
a_new = []
for i in a:
i = i.split(",") #遍历a列表将每个列表元素按,分隔成列表
a_new.append(i) #生成新的列表
# print (a_new)
header = a_new[0] #取新列表第一行表头
a_new.remove(header) #删除列表的第一行表头
f.close()
while True:
sr = input("请输入查询数据:").strip()
print(header)
# 模糊查询
if sr in e:
counts = e.count(sr)
for line in a:
if sr in line:
print(line.split(","))
print(f"共检索到{counts}处")
else:
flag = re.split("[<>=]", sr)
if len(flag) != 2:
print("输入公式错误!")
continue
cloumnid, jg = flag
ids = header.index(cloumnid)
# print(ids)
if cloumnid not in header:
print("请输入正确的查询名!")
continue
# if not jg.replace(".", "", 1).isdigit():
# print("输入数字有误!")
# continue
try:
val=float(jg)
except:
print("输入的数值不合法,重新输入!")
continue
icount=[]
if ">" in sr:
for i in a_new:
if "亿" in i[ids]:
c = float(i[ids][:-1]) * 10000
else:
c = float(i[ids].strip("万").strip("%").strip())
if c > float(jg):
icount.append(i)
print(i)
print(f"共检索到{len(icount)}处。")
if "<" in sr:
for i in a_new:
if "亿" in i[ids]:
c = float(i[ids][:-1]) * 10000
else:
c = float(i[ids].strip("万").strip("%").strip())
if c < float(jg):
icount.append(i)
print(i)
print(f"共检索到{len(icount)}处。")
if "=" in sr:
for i in a_new:
if "亿" in i[ids]:
c = float(i[ids][:-1]) * 10000
else:
c=float(i[ids].strip("万").strip("%").strip())
if c == float(jg):
icount.append(i)
print(i)
print(f"共检索到{len(icount)}处。")
读取股票文本信息并查询Python
最新推荐文章于 2024-07-20 17:12:48 发布