复旦大学Python程序设计大作业(略有修改)

要求:分析某水果连锁店的csv格式进货和销售数据(UTF-8 编码),用户输入数据所在文件夹(例如 D:\data),在该数据文件夹下面 有in、out两个文件夹。连锁店有很多家,每个连锁店有三位数的编号。每家连锁店有以编号为名的2个文件分别存放在上述两个文件夹里,例如:123号连锁店在in文件夹里有123in.csv,在out文件夹里有123out.csv。
文件样例:

123in.csv内容如下:
订单号,日期,商品名称,进货价格,进货数量
1001,2023-09-01,苹果,2.2,10
1002,2023-09-05,苹果,2.7,8
1003,2023-09-06,香蕉,2.9,10
1004,2023-10-01,香蕉,2.8,5
1005,2023-11-03,苹果,2.5,6
1007,2024-01-03,苹果,2.5,4
1008,2024-01-03,苹果,2.5,8

321in.csv内容如下:
订单号,日期,商品名称,进货价格,进货数量
1001,2023-09-04,苹果,2.2,15
1002,2023-10-04,橙子,2.6,25
1003,2023-11-04,苹果,2.7,10
1004,2023-12-04,苹果,2.5,5
1005,2024-01-02,橙子,2.7,6
1006,2024-01-05,香蕉,2.7,15

123out.csv 内容如下:
订单号,日期,商品名称,销售价格,销售数量
1001,2023-09-14,苹果,3.5,18
1002,2023-09-15,香蕉,4.2,5
1003,2023-10-14,苹果,3.5,5
1004,2023-11-05,香蕉,4,3
1005,2023-12-14,橙子,3.6,3
1006,2024-01-05,苹果,3.5,10

321out.csv 内容如下:
订单号,日期,商品名称,销售价格,销售数量
1001,2023-09-13,苹果,3.3,4
1002,2023-10-15,橙子,4,20
1003,2023-11-13,苹果,3.5,10
1005,2023-12-18,苹果,4,1
1006,2024-01-15,橙子,4,5
1007,2024-01-15,香蕉,4,15
请分析每家连锁店的上述两个文件,进货成本=进货价格*进货数 量,销售额=销售价格*销售数量,利润=销售额-进货成本。计算得出:
1.按时间顺序输出每月(格式xxxx年xx月)利润最大的连锁店编号。
2.根据所有连锁店的总销售额,按时间顺序输出每月(格式xxxx年xx月)销售最好的商品名称。


样例数据文件对应的输出示例为:
2023年09月:123连锁店利润最大,商品苹果销售最好
2023年10月:321连锁店利润最大,商品橙子销售最好
2023年11月:321连锁店利润最大,商品苹果销售最好
2023年12月:123连锁店利润最大,商品橙子销售最好
2024年01月:321连锁店利润最大,商品香蕉销售最好

完整代码如下:

import os
storename=os.listdir(".//out/")  #商店名称列表
best=dict()#销售最好的商品
profit=dict()#利润最好的商店
for x in range(0,len(storename)): #打开所有文件
    store_id=str(storename[x])[0:3] #店序号
    with open(".//out/"+str(store_id)+"out.csv", 'r',encoding="UTF-8") as file: #依次循环打开店序号对应的销售文件
        raw_data=file.readlines()#读取销售文件
        remove = ['\n']  #去掉空值
        shopping= [x for x in raw_data if x not in remove]
        file_in=open(".//in/"+str(store_id)+"in.csv", 'r',encoding="UTF-8") #打开店序号对应的进货文件
        shopping_in=file_in.readlines() 
        for line in shopping[1:]:
            sell_date=line.split(",")[1][0:7] #销售日期(到月)
            sell_name=line.split(",")[2] #销售名称
            sell_price=line.split(",")[3] #销售价格
            sell_count=line.split(",")[4] #销售数量
            if sell_date in best: #按月统计销售最好的商品
                if sell_name in best[sell_date]: #若有重复商品,累加
                    best[sell_date].update({sell_name:best[sell_date][sell_name]+float(sell_price)*float(sell_count)})
                else: #若没有,新加
                    best[sell_date].update({sell_name:float(sell_price)*float(sell_count)})
            else:#没有月商品销售数据,新建
                best[sell_date]={sell_name:float(sell_price)*float(sell_count)}   
            if sell_date in profit: #统计销售最好的商店
                if store_id in profit[sell_date]: #若有此店,累加
                    profit[sell_date].update({store_id:profit[sell_date][store_id]+float(sell_price)*float(sell_count)})
                else:#若无此店,新建
                    profit[sell_date].update({store_id:float(sell_price)*float(sell_count)})
            else:#无日期
                profit[sell_date]={store_id:float(sell_price)*float(sell_count)}
        for line_in in shopping_in[1:]:
            buy_date=line_in.split(",")[1][0:7] #购入日期(到月)
            buy_name=line_in.split(",")[2] #购入名称
            buy_price=line_in.split(",")[3] #购入价格
            buy_count=line_in.split(",")[4] #购入数量
            if buy_date in profit: #统计利润最好的商店
                if store_id in profit[buy_date]:#日期内有此店
                    profit[buy_date].update({store_id:round(profit[buy_date][store_id]-float(buy_price)*float(buy_count),2)})
                else:#日期内无此店
                    for storeid in profit[buy_date].keys():
                        profit[buy_date]={storeid:profit[buy_date][storeid]}|{store_id:round(0-float(buy_price)*float(buy_count),2)}          
            else:#无日期情形
                profit[buy_date]={store_id:round(0-float(buy_price)*float(buy_count),2)}
        file_in.close()
    file.close()
for i in best.keys():#取极值
    t_best=-1000  #设销售初值
    t_profit=-1000 #设利润初值
    for v_best in best[i].values():  #依次比较销售最好
        t_best=v_best if (t_best<v_best) else t_best
    for v_profit in profit[i].values(): #依次比较利润最好
        t_profit=v_profit if (t_profit<v_profit) else t_profit
    keys=list(best[i].keys())[list(best[i].values()).index(t_best)]
    fits=list(profit[i].keys())[list(profit[i].values()).index(t_profit)]
    print(i[0:4]+'年'+i[5:]+"月:"+fits+"连锁店利润最大,商品"+keys+"销售最好") 

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SDAU2005

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值