import pandas as pd file_path = input(r"请输入文件路径:") sheet_name = "货机数据" try: air_data = pd.read_excel(file_path,usecols=["结算账期","结算网点代码","发货日期","供应商名称","发货结算货物类型","结算重量","实际支出"]) # 设置样式 styled_df = air_data.style \ .set_properties(**{'text-align': 'center'}) \ .set_table_styles([{'selector': 'td', 'props': [('min-width', '50px')]}]) \ .set_table_attributes('border="1" cellspacing="0" cellpadding="4"') with pd.ExcelWriter("场地效益看板 (test).xlsx", engine='openpyxl', mode='a', if_sheet_exists='replace') as writer: try: # 如果子表存在,则覆盖原子表 writer.book[sheet_name] writer.sheet = {ws.title: ws for ws in writer.book.worksheets} except KeyError: pass styled_df.to_excel(writer, sheet_name=sheet_name, startcol=0,index=False) print("完成清洗") except Exception as e: print("发生了异常:", e)帮我修改这段代码 错误信息是发生了异常: [Errno 22] Invalid argument: '"d:\user\01429213\desktop\数据原表\数据底表\散航发货结算按供应商导出871Y6月.xlsx"'
时间: 2023-07-19 19:39:16 浏览: 49
这段代码中出现了一个错误,是因为文件路径中的反斜杠被转义了,可以将输入的路径中的反斜杠替换成正斜杠来解决这个问题。
可以使用以下代码来替换输入路径中的反斜杠:
```
file_path = file_path.replace("\\", "/")
```
这个代码应该放在输入路径的代码行后面,如下所示:
```
file_path = input(r"请输入文件路径:")
file_path = file_path.replace("\\", "/") # 添加这一行
sheet_name = "货机数据"
try:
air_data = pd.read_excel(file_path,usecols=["结算账期","结算网点代码","发货日期","供应商名称","发货结算货物类型","结算重量","实际支出"])
# 设置样式
styled_df = air_data.style \
.set_properties(**{'text-align': 'center'}) \
.set_table_styles([{'selector': 'td', 'props': [('min-width', '50px')]}]) \
.set_table_attributes('border="1" cellspacing="0" cellpadding="4"')
with pd.ExcelWriter("场地效益看板 (test).xlsx", engine='openpyxl', mode='a', if_sheet_exists='replace') as writer:
try:
# 如果子表存在,则覆盖原子表
writer.book[sheet_name]
writer.sheet = {ws.title: ws for ws in writer.book.worksheets}
except KeyError:
pass
styled_df.to_excel(writer, sheet_name=sheet_name, startcol=0,index=False)
print("完成清洗")
except Exception as e:
print("发生了异常:", e)
```
这样应该就能解决这个问题了。