代码:import os import pandas as pd print("开始计算一口价") folder_path = "D:/一口价生成" file_name = "活动.xlsx" new_file_name = "一口价及升价4.xlsx" activity_file_path = os.path.join(folder_path, file_name) activity_df = pd.read_excel(activity_file_path) discounts = {"6.9折": 0.69, "5折": 0.5, "3.9折": 0.39} new_price_df = pd.DataFrame(columns=["商品编码", "价格"]) for index, row in activity_df.iterrows(): if row["活动"] in discounts: standard_price = row["标准售价"] discount = discounts.get(row["活动"], 1) new_price = round(standard_price * discount, 2) product_code = str(row["商品编码"]).zfill(7) new_price_df.loc[index] = [product_code, new_price] elif isinstance(row["活动"], int): product_code = str(row["商品编码"]).zfill(7) new_price = int(row["活动"]) new_price_df.loc[index] = [product_code, new_price] elif isinstance(row["活动"], float): product_code = str(row["商品编码"]).zfill(7) new_price = float(row["活动"]) new_price_df.loc[index] =[product_code, new_price] new_file_path = os.path.join(folder_path, new_file_name) new_price_df.to_excel(new_file_path, index=False) print("已生成") 新增一个功能:将“活动”列中命名为“买一送一(系统设置为单件5折)”对应的“标准售价”*05得出新的数值,将“买一送一(系统设置为单件5折)”对应的“商品编号”和计算出来的数值输出到一个新的xlsx表格,表格命名为“5折”,其中表格A列为“商品编号”对应“商品编号”,B列为“价格,对应计算出来的数值。而表格保存在“活动”表格的同一个目录下
时间: 2024-03-06 17:50:39 浏览: 25
代码修改如下:
```python
import os
import pandas as pd
print("开始计算一口价")
folder_path = "D:/一口价生成"
file_name = "活动.xlsx"
new_file_name = "一口价及升价4.xlsx"
activity_file_path = os.path.join(folder_path, file_name)
activity_df = pd.read_excel(activity_file_path)
discounts = {"6.9折": 0.69, "5折": 0.5, "3.9折": 0.39}
new_price_df = pd.DataFrame(columns=["商品编码", "价格"])
new_5_discount_df = pd.DataFrame(columns=["商品编码", "价格"])
for index, row in activity_df.iterrows():
if row["活动"] in discounts:
standard_price = row["标准售价"]
discount = discounts.get(row["活动"], 1)
new_price = round(standard_price * discount, 2)
product_code = str(row["商品编码"]).zfill(7)
new_price_df.loc[index] = [product_code, new_price]
elif row["活动"] == "买一送一(系统设置为单件5折)":
standard_price = row["标准售价"]
new_price = round(standard_price * 0.5, 2)
product_code = str(row["商品编码"]).zfill(7)
new_5_discount_df.loc[index] = [product_code, new_price]
elif isinstance(row["活动"], int):
product_code = str(row["商品编码"]).zfill(7)
new_price = int(row["活动"])
new_price_df.loc[index] = [product_code, new_price]
elif isinstance(row["活动"], float):
product_code = str(row["商品编码"]).zfill(7)
new_price = float(row["活动"])
new_price_df.loc[index] =[product_code, new_price]
new_file_path = os.path.join(folder_path, new_file_name)
new_price_df.to_excel(new_file_path, index=False)
new_5_discount_file_name = "5折.xlsx"
new_5_discount_file_path = os.path.join(folder_path, new_5_discount_file_name)
new_5_discount_df.to_excel(new_5_discount_file_path, index=False)
print("已生成")
```
新添加的功能会将“活动”列中命名为“买一送一(系统设置为单件5折)”对应的“标准售价”乘以0.5得出新的数值,将“买一送一(系统设置为单件5折)”对应的“商品编号”和计算出来的数值输出到一个新的xlsx表格。表格命名为“5折”,其中表格A列为“商品编号”对应“商品编号”,B列为“价格”,对应计算出来的数值。新的xlsx表格保存在“活动”表格的同一个目录下。