代码:import os import pandas as pd print("开始计算一口价") folder_path = "D:/一口价生成" file_name = "活动.xlsx" new_file_name = "一口价及升价.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] else: product_code = str(row["商品编码"]).zfill(7) try: new_price = int(row["活动"]) except ValueError: # 处理折扣率类型 if "折" in row["活动"]: discount = float(row["活动"].strip("折")) / 10 new_price = round(row["标准售价"] * discount, 2) else: 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("已生成") 无法将 "活动" 列中的int类型和float类型的数值输出到“一口价”的表格上,请修改代码,需要“活动”列中的int和float类型的值输出。
时间: 2024-03-06 19:50:05 浏览: 89
引脚描述-详解pandas库pd.read_excel操作读取excel文件参数整理与实例
可以尝试将代码中的以下部分:
```
try:
new_price = int(row["活动"])
except ValueError:
if "折" in row["活动"]:
discount = float(row["活动"].strip("折")) / 10
new_price = round(row["标准售价"] * discount, 2)
else:
new_price = float(row["活动"])
```
改为如下:
```
if type(row["活动"]) == int or type(row["活动"]) == float:
new_price = row["活动"]
elif "折" in row["活动"]:
discount = float(row["活动"].strip("折")) / 10
new_price = round(row["标准售价"] * discount, 2)
else:
new_price = float(row["活动"])
```
这样可以将 int 和 float 类型的值直接赋值给新价格列。
阅读全文