import pandas as pd # file_path = input(r"请输入文件路径:").strip() # file_path = file_path.replace("\\","\\\\") file_path = "散航发货结算按供应商导出 -871Y (2).xlsx" 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)
时间: 2024-01-03 09:04:57 浏览: 24
根据你提供的代码,可能会遇到文件读取失败的问题。请确保文件路径和文件名正确,并且文件存在且可读。你可以尝试打印出`file_path`的值来确认文件路径是否正确,例如:
```
print(file_path)
```
如果文件路径有误,你可以手动修改`file_path`的值,将其指向正确的文件路径。另外,你也可以考虑使用绝对路径来读取文件,以避免相对路径出现问题。
相关问题
import pandas as pd file_path = input(r"请输入文件路径:") # file_path = file_path.replace("\\","\\") # file_path = r"d:\user\01429213\desktop\数据原表\数据底表\散航发货结算按供应商导出871Y6月.xlsx" 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)帮我修改使他不会报错
在你的代码中,有一些问题需要修正:
1. 第一行的 `import pandas as pd` 后面缺少了换行符,应该加上。
2. 第二行的 `file_path = input(r"请输入文件路径:")` 可能会出现字符串格式转换错误,可以将其修改为:
```
file_path = input("请输入文件路径:").strip()
```
这里使用 `strip()` 方法可以去除用户输入的路径字符串两端的空白字符,避免路径错误。
3. 第7行的 `usecols` 参数应该是一个列表,需要将其修改为:
```
usecols=["结算账期","结算网点代码","发货日期","供应商名称","发货结算货物类型","结算重量","实际支出"]
```
这里需要将参数用方括号 `[ ]` 括起来,表示一个列表。
4. 第17行的 `writer.sheet = {ws.title: ws for ws in writer.book.worksheets}` 应该改为:
```
writer.sheets = {ws.title: ws for ws in writer.book.worksheets}
```
这里需要将 `sheet` 改为 `sheets`,表示一个字典,以便更新已有的工作表。
综合修改后的代码如下所示:
```
import pandas as pd
file_path = input("请输入文件路径:").strip()
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.sheets = {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)
```
import pandas as pd file_path = input(r"请输入文件路径:").strip() file_path = file_path.replace("\\","\\\\") # file_path = r"d:\user\01429213\desktop\数据原表\数据底表\散航发货结算按供应商导出871Y6月.xlsx" 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)详细的带吗
您的代码中,首先需要将文件路径中的反斜杠转义为双反斜杠,这一部分已经实现了。不过,您在 ExcelWriter 时,使用了 "mode='a'",表示以追加的方式打开 Excel 文件,这可能会导致出现问题。如果文件不存在,则会创建一个新文件,如果文件已经存在,则会将数据追加到文件的末尾。但是,由于模式为追加,所以新写入的数据可能会覆盖原有的数据,导致结果不正确。
为了避免这个问题,可以将 "mode='a'" 修改为 "mode='w'",表示以覆盖的方式打开 Excel 文件。这样,如果文件不存在,则会创建一个新文件,如果文件已经存在,则会覆盖原有的数据。
修改后的代码如下所示:
```python
import pandas as pd
file_path = input(r"请输入文件路径:").strip()
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='w', 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)
```
希望这个解决方案能够帮助到您!如果您还有其他问题,欢迎随时提出。