读取股票文本信息并查询Python

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)}处。")

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值