欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【Python作业】对股票数据分析&处理

程序员文章站 2022-06-04 17:18:08
...

一、前言

上午写了一个版本,将简单问题复杂化了,本题主要考的是 文件操作及常见数据类型的处理 ,惭愧学了几个月基础还是很差,想想还是觉得不对劲,重新写了一个版本,精简不少。

版本1:Python综合实战对股票数据分析&处理

二、 题目要求

开发程序对stock_data.txt进行以下操作:

  1. 程序启动后,给⽤用户提供查询接⼝口,允许⽤用户重复查股票⾏行行情信息(⽤用到循环)
  2. 允许用户通过模糊查询股票名,⽐如输入“啤酒”, 就把所有股票名称中包含“啤酒”的信息打印出来
  3. 允许按股票价格、涨跌幅、换手率这⼏列来筛选信息,⽐如输入“价格>50”则把价格大于50的股票都打印,输⼊入“市盈率<50“,则把市盈率小于50的股票都打印,不用判断等于。

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

三、 原数据

股票代码,股票名称,当前价,涨跌额,涨跌幅,年初至今,成交量,成交额,换手率,市盈率(TTM),股息率,市值
SH601778,N晶科,6.29,+1.92,+43.94%,+43.94%,259.66,1625.52,0.44%,22.32,-,173.95亿
SH688566,吉贝尔,52.66,+6.96,+15.23%,+122.29%,1626.58,8.09亿,42.29%,89.34,-,98.44亿
SH688268,华特气体,88.80,+11.72,+15.20%,+102.51%,622.60,5.13亿,22.87%,150.47,-,106.56亿
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亿
SZ000615,京汉股份,4.61,+0.42,+10.02%,+4.06%,518.09,2388.41,0.69%,亏损,0.00%,36.06亿
SZ300443,金雷股份,18.33,+1.67,+10.02%,+32.74%,987.25,1.75亿,6.19%,18.78,1.08%,43.64亿
SH600506,香梨股份,11.42,+1.04,+10.02%,+11.96%,1039.48,1.18亿,7.04%,亏损,0.00%,16.87亿
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亿
SZ002449,国星光电,11.92,+1.08,+9.96%,-6.80%,5185.52,6.03亿,8.53%,21.51,2.73%,73.72亿

三、代码示例

'''
 股票数据分析
 version:02
 author:金鞍少年
 date:2020-10-21
'''

def stocks(file):
    stocks_data = {}
    Col_list = ['当前价', '涨跌额', '涨跌幅', '换手率']
    with open(file, 'r', encoding='utf-8')as f:
        # 读取文件
        for line in f:
            line_list = line.strip().split(',')  # 按输入字符分割,将其转化为数组
            stocks_data[line_list[0]] = line_list
        headers = stocks_data["股票代码"]

        while True:
            # 模糊查询
            try:
                count = 0
                cmd = input("\n=========股票数据查询系统==========\n请输入查询指令>>:").strip()
                if not cmd: continue
                print(headers)  # 打印头部

                # 模糊查询
                for sid_s, s_data in stocks_data.items():
                    s_name = s_data[1]
                    if cmd in s_name:
                        print(s_data)
                        count += 1

                # 公式查询
                res = re.split("[<>]", cmd)
                if res[0] in Col_list:
                    for sid_s, s_data in stocks_data.items():
                        stock_info = s_data[headers.index(res[0])].replace('%', '')
                        res_info = res[1]
                        if sid_s == "股票代码": continue
                        if ">" in cmd:
                            if float(stock_info) > float(res_info):
                                print(s_data)
                                count += 1
                        elif "<" in cmd:
                            if float(stock_info) < float(res_info):
                                print(s_data)
                                count += 1
                print(f"总共找到 {count} 条数据!")
            except Exception as e:
                print(f"出现错误:{e}")

if __name__ == '__main__':
    import re
    stocks(file="../res/stock_data.txt")

四、结果

【Python作业】对股票数据分析&处理