Python综合实战-股票数据分析&处理(查询接口)

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

  1. 程序启动后,给⽤用户提供查询接⼝口,允许⽤用户重复查股票⾏行行情信息(⽤用到循环)
  2. 允许⽤用户通过模糊查询股票名,⽐比如输⼊入“啤酒”, 就把所有股票名称中包含“啤酒”的信息打印出来
  3. 允许按股票价格、涨跌幅、换⼿手率这⼏几列列来筛选信息,⽐比如输⼊入“价格>50”则把价格⼤大于50的股票
    都打印,输⼊入“市盈率<50“,则把市盈率⼩小于50的股票都打印,不不⽤用判断等于。
    思路路提示:加载⽂文件内容到内存,转成dict or list结构,然后对dict or list 进⾏行行查询等操作。 这样以后
    就不不⽤用每查⼀一次就要打开⼀一次⽂文件了了,效率会⾼高
list_data=[]
#用来判断该列字符串最后一位是不是不是数字。
q_list=[4,5,6,8,10]
#用于处理两列单位不一致的
d_list=[7,11]
#将数据转化成一个列表
with open('g_data','r',encoding='utf-8') as f:
	#股票参数名列表
    list_name=f.readline().strip('\n').split(',')
    for line in f:
        line = line.strip('\n')
        list_data.append(line.split(','))
while True:
    s= input("股票查询接口>>").strip()
    print(list_name)
    if '>' in s:
        #命令分割
        index= s.index('>')
        name=s[0:index]
        date=s[index+1:]
        #获取所查的列数
        l_index=list_name.index(name)
        #处理列有百分号的情况
        if l_index in q_list:
                for l in list_data:
                    try:
                        if float(l[l_index][0:-1]) > float(date):
                            print(l)

                    except ValueError as e:
                        continue
            #处理两列单位不同的情况
        elif l_index in d_list:
            for l in list_data:
                if '亿' in l[l_index]:
                    if float(l[l_index][0:-1]) * 10000 > float(date):
                        print(l)

                if float(l[l_index][0:-1]) > float(date):
                    print(l)
        else:
            for l in list_data:
                try:
                    if float(l[l_index]) > float(date):
                        print(l)
                except ValueError as e:
                    continue
    #处理小于情况
    elif '<' in s:
        index = s.index('<')
        name = s[0:index]
        date = s[index + 1:]
        # 获取所查的列数
        l_index = list_name.index(name)
        # 处理列有百分号的情况
        if l_index in q_list:
            for l in list_data:
                try:
                    if float(l[l_index][0:-1]) < float(date):
                        print(l)
                except ValueError as e:
                    continue
        # 处理两列单位不同的情况
        elif l_index in d_list:
            for l in list_data:
                if '亿' in l[l_index]:
                    if float(l[l_index][0:-1]) * 10000 < float(date):
                        print(l)
                if float(l[l_index][0:-1]) < float(date):
                    print(l)
        else:
            for l in list_data:
                try:
                    if float(l[l_index]) < float(date):
                        print(l)
                except ValueError as e:
                    print(l)
    # 模糊查询
    else:
        for l in list_data:
            if s in l[1]:
                print(l)

测试结果如下:注意金钱单位为万
Python综合实战-股票数据分析&处理(查询接口)_第1张图片
Python综合实战-股票数据分析&处理(查询接口)_第2张图片
Python综合实战-股票数据分析&处理(查询接口)_第3张图片
股票数据如下:

股票代码,股票名称,当前价,涨跌额,涨跌幅,年初至今,成交量,成交额,换手率,市盈率(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亿

你可能感兴趣的:(python,python,接口)