file_path = input(r"请输入文件路径:").strip()会报错,错误信息请输入文件路径:"d:\user\01429213\desktop\数据原表\数据底表\散航发货结算按供应商导出871Y6月.xlsx" 发生了异常: [Errno 22] Invalid argument: '"d:\user\01429213\desktop\数据原表\数据底表\散航发货结算按供应商导出871Y6月.xlsx"'
时间: 2023-07-20 21:42:46 浏览: 38
出现这个错误的原因是因为在 Windows 系统中,"\U" 被解释为 Unicode 转义字符,而你的路径中包含了 "\U",这会导致 Python 解析路径时出错。
为了解决这个问题,你可以在文件路径前面加上一个 "r",将其转换为原始字符串。例如,可以将代码修改为:
```python
file_path = input(r"请输入文件路径:").strip()
```
这样,Python 就会将路径中的所有字符都按照字面意义来解析,而不会将其解释为转义字符。
相关问题
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)
```
希望这个解决方案能够帮助到您!如果您还有其他问题,欢迎随时提出。