努力努力再努Li

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
# coding: utf-8

"""

作业题目:股票信息查询程序
    作业需求:
    1 程序启动后,给用户提供查询接口,允许用户重复查股票行情信息(用到循环)
    2 允许用户通过模糊查询股票名,比如输入“啤酒”, 就把所有股票名称中包含“啤酒”的信息打印出来
    3 允许按股票价格、涨跌幅、换手率这几列来筛选信息,
         比如输入“价格>50”则把价格大于50的股票都打印,输入“市盈率<50“,则把市盈率小于50的股票都打印,不用判断等于。
    stock_data.txt 此文件请从课件中下载。

思路提示:
加载文件内容到内存,转成dict or list结构,然后对dict or list 进行查询等操作。 这样以后就不用每查一次就要打开一次文件了,效率会高。

知识点:
",".join(str.split()) 把列表转为字符串。由于空格做分隔,可能会受到文字中包含空格的影响,建议把空格转换为逗号,通过逗号做分隔,
str.strip() 把字符串中的特殊字符(比如\n换行给去除)
exit("程序退出,谢谢使用!")  可以直接退出程序(break是退出当前循环)

"""

# 首先处理文本文件,把空格转换为逗号
"""

# 读取文件
with open("stock_data_dict_空格改逗号分隔.txt", mode="r", encoding="utf-8") as f:
    # 设置开始读取位置的下标为0
    f.seek(0)
    # 读取原文件的信息,list列表
    read_stock_data = f.readlines()
    # 存放修改后的信息
    modified_stock_data = ""
    # 修改信息,循环列表每1行
    for line in read_stock_data:
        # 把该行空格转换为逗号,并赋值(注意保留换行)
        modified_stock_data += ",".join(line.split()) + "\n"

# 写入文件
with open("stock_data_dict_空格改逗号分隔.txt", mode="w", encoding="utf-8") as f:
    # 把前边修改后的信息,写入文件
    f.write(modified_stock_data)

"""

# 定义变量
# 通过字典存放股票信息
stock_data_dict = {}
# # 判断是否退出循环
# is_not_exit = True

# 读取文件
with open("stock_data_dict_空格改逗号分隔.txt", mode="r", encoding="utf-8") as f:
    # 把首行标题单独取出,放入list列表
    stock_data_title = f.readline().strip().split(",")
    # 读取文件中的股票信息(不含标题),并存入字典
    for line in f.readlines():
        # 把取出的字符串转为列表
        line = line.strip().split(",")
        # 把该行添加到字典,列表中的“名称”为key,列表为value
        stock_data_dict[line[2]] = line

# while循环实现股票查询逻辑
while True:
    # 查询到的股票记录数
    record_num = 0
    # 打印标题,供用户选择
    print(stock_data_title)
    # 接收用户输入条件:模糊查询-股票名称,表达式-价格>50,表达式-市盈率<50,注意百分号%处理
    user_input = input("股票查询接口>>")

    # for循环遍历所有股票信息
    for stock_key,stock_value in stock_data_dict.items():
        # 模糊查询-股票名称,存在则打印(title已单独取出,dict.keys()中存的是股票名称)
        if user_input in stock_key:
            print(stock_value)
            record_num += 1

        # 如果用户输入条件,包含>,则按照表达式处理
        elif ">" in user_input:
            # 把用户输入转为列表
            user_input_list = user_input.strip().split(">")
            # 用户输入的标题
            user_input_title = user_input_list[0]
            # 用户输入的值,如果存在百分号%则去除
            user_input_value = user_input_list[1].strip("%")
            # 用户输入的列索引
            user_input_index = stock_data_title.index(user_input_title)
            # 当前遍历所在行,对应列的值,如果存在百分号%则去除
            column_value = stock_value[user_input_index].strip("%")
            # 判断用户输入,符合条件则打印
            if float(column_value) > float(user_input_value):
                print(stock_value)
                record_num += 1

        # 如果用户输入条件,包含<,则按照表达式处理
        elif "<" in user_input:
            # 把用户输入转为列表
            user_input_list = user_input.strip().split("<")
            # 用户输入的标题
            user_input_title = user_input_list[0]
            # 用户输入的值,如果存在百分号%则去除
            user_input_value = user_input_list[1].strip("%")
            # 用户输入的列索引
            user_input_index = stock_data_title.index(user_input_title)
            # 当前遍历所在行,对应列的值,如果存在百分号%则去除
            column_value = stock_value[user_input_index].strip("%")
            # 判断用户输入,符合条件则打印
            if float(column_value) < float(user_input_value):
                print(stock_value)
                record_num += 1

        # 退出,输入q
        elif user_input == "q":
            # is_not_exit = False
            exit("程序退出,谢谢使用!")

    # 打印找到的股票记录条数
    print("找到{0}条".format(record_num))

 

posted on 2020-09-13 17:43  努力努力再努Liz  阅读(336)  评论(0编辑  收藏  举报