import pandas as pd
dataset = pd.read_excel(r'D:\a.xlsx', sheet_name='Sheet1')
print(dataset)
出现报错信息:
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
Cell In[39], line 2
1 import pandas as pd
----> 2 dataset = pd.read_excel(r'D:\a.xlsx', sheet_name='Sheet1')
3 print(dataset)
File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\excel\_base.py:495, in read_excel(io, sheet_name, header, names, index_col, usecols, dtype, engine, converters, true_values, false_values, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, parse_dates, date_parser, date_format, thousands, decimal, comment, skipfooter, storage_options, dtype_backend, engine_kwargs)
493 if not isinstance(io, ExcelFile):
494 should_close = True
--> 495 io = ExcelFile(
496 io,
497 storage_options=storage_options,
498 engine=engine,
499 engine_kwargs=engine_kwargs,
500 )
501 elif engine and engine != io.engine:
502 raise ValueError(
503 "Engine should not be specified when passing "
504 "an ExcelFile - ExcelFile already has the engine set"
505 )
File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\excel\_base.py:1550, in ExcelFile.__init__(self, path_or_buffer, engine, storage_options, engine_kwargs)
1548 ext = "xls"
1549 else:
-> 1550 ext = inspect_excel_format(
1551 content_or_path=path_or_buffer, storage_options=storage_options
1552 )
1553 if ext is None:
1554 raise ValueError(
1555 "Excel file format cannot be determined, you must specify "
1556 "an engine manually."
1557 )
File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\excel\_base.py:1402, in inspect_excel_format(content_or_path, storage_options)
1399 if isinstance(content_or_path, bytes):
1400 content_or_path = BytesIO(content_or_path)
-> 1402 with get_handle(
1403 content_or_path, "rb", storage_options=storage_options, is_text=False
1404 ) as handle:
1405 stream = handle.handle
1406 stream.seek(0)
File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\common.py:882, in get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)
873 handle = open(
874 handle,
875 ioargs.mode,
(...)
878 newline="",
879 )
880 else:
881 # Binary mode
--> 882 handle = open(handle, ioargs.mode)
883 handles.append(handle)
885 # Convert BytesIO or file objects passed with an encoding
OSError: [Errno 22] Invalid argument: '\u202aD:\\a.xlsx'
出现问题的原因是,之前写代码的时候,文件的读取路径是
“点击目标文件右键-》复制安全模块里面的路径
”,然后直接粘贴在代码中。
经网上查阅资料,说这样操作会导致路径首部被自动填充一个空格导致路径错误。但查看代码并看不出有空格的情况。
解决的方法
:尝试过将第一个字符删掉重新输入运行,仍然报一样的错误。后面将
文件路径全部删掉,手动重新输入即可
。
代码修改后如下:(两份代码看不出差别)
import pandas as pd
dataset = pd.read_excel(r'D:\a.xlsx', sheet_name='Sheet1')
print(dataset)
运行后显示:
Empty DataFrame
Columns: [111, 222, 333, 444]
Index: []