如何在iPhone的Carnets App中通过Jupyter Notebook导入.xlsx文件?
在iPhone的Carnets App中读取Excel文件的路径问题
我想在iPhone的Carnets App里的Jupyter Notebook处理从Kaggle下载的数据集,但一直找不到正确的文件路径,试了两段代码都报错。
尝试过的代码
第一段代码
import pandas as pd aguacates = pd.read_excel("En mi iPhone ▸ video141_python_pildorasInfor ▸ Aguacate producción.xlsx") print(aguacates)
第二段代码
import pandas as pd aguacates = pd.read_excel("En mi iPhone \\video141_python_pildorasInfor\\Aguacate producción.xlsx") (aguacates)
报错信息
FileNotFoundError Traceback (most recent call last) Cell In[2], line 3 1 import pandas as pd ----> 3 aguacates = pd.read_excel("En mi iPhone\\video141_python_pildorasInfor\\Aguacate producción.xlsx") 4 print(aguacates) File /private/var/containers/Bundle/Application/D169301A-0DB7-4982-8FE3-9C6D0E0C17D8/Carnets.app/Library/lib/python3.11/site-packages/pandas/io/excel/_base.py:478, 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) 476 if not isinstance(io, ExcelFile): 477 should_close = True --> 478 io = ExcelFile(io, storage_options=storage_options, engine=engine) 479 elif engine and engine != io.engine: 480 raise ValueError( 481 "Engine should not be specified when passing " 482 "an ExcelFile - ExcelFile already has the engine set" 483 ) File /private/var/containers/Bundle/Application/D169301A-0DB7-4982-8FE3-9C6D0E0C17D8/Carnets.app/Library/lib/python3.11/site-packages/pandas/io/excel/_base.py:1496, in ExcelFile.__init__(self, path_or_buffer, engine, storage_options) 1494 ext = "xls" 1495 else: --> 1496 ext = inspect_excel_format( 1497 content_or_path=path_or_buffer, storage_options=storage_options 1498 ) 1499 if ext is None: 1500 raise ValueError( 1501 "Excel file format cannot be determined, you must specify " 1502 "an engine manually." 1503 ) File /private/var/containers/Bundle/Application/D169301A-0DB7-4982-8FE3-9C6D0E0C17D8/Carnets.app/Library/lib/python3.11/site-packages/pandas/io/excel/_base.py:1371, in inspect_excel_format(content_or_path, storage_options) 1368 if isinstance(content_or_path, bytes): 1369 content_or_path = BytesIO(content_or_path) --> 1371 with get_handle( 1372 content_or_path, "rb", storage_options=storage_options, is_text=False 1373 ) as handle: 1374 stream = handle.handle 1375 stream.seek(0) File /private/var/containers/Bundle/Application/D169301A-0DB7-4982-8FE3-9C6D0E0C17D8/Carnets.app/Library/lib/python3.11/site-packages/pandas/io/common.py:868, in get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options) 859 handle = open( 860 handle, 861 ioargs.mode, (...) 864 newline="", 865 ) 866 else: 867 # Binary mode --> 868 handle = open(handle, ioargs.mode) 869 handles.append(handle) 871 # Convert BytesIO or file objects passed with an encoding FileNotFoundError: [Errno 2] No such file or directory: '\u2068\u2068En mi iPhone\u2069\\\u2068video141_python_pildorasInfor\u2069\\Aguacate producción.xlsx'
解决方法
- 清理路径中的隐藏字符:错误信息显示路径里包含
\u2068和\u2069这类不可见的格式化字符,这是从文件管理器复制路径时带入的。直接手动输入路径,避免复制粘贴。 - 导入文件到Carnets工作目录(最简便):
- 打开Carnets App,点击右上角
+按钮,选择Import - 找到存储数据集的文件夹,选中
Aguacate producción.xlsx完成导入 - 导入后直接用文件名读取:
import pandas as pd aguacates = pd.read_excel("Aguacate producción.xlsx") print(aguacates)
- 打开Carnets App,点击右上角
- 使用正确的iOS文件路径:如果要读取外部文件夹的文件,需确保Carnets有该文件夹的访问权限,并用iOS原生路径格式:
也可以用import pandas as pd # 替换为实际的文件路径,可通过Carnets的文件浏览器查看 aguacates = pd.read_excel("/private/var/mobile/Containers/Data/Documents/video141_python_pildorasInfor/Aguacate producción.xlsx")pathlib模块更安全地构建路径:from pathlib import Path import pandas as pd data_folder = Path("/private/var/mobile/Containers/Data/Documents/video141_python_pildorasInfor") file_path = data_folder / "Aguacate producción.xlsx" aguacates = pd.read_excel(file_path) print(aguacates) - 检查文件名和路径拼写:确认文件名、文件夹名的拼写、空格、特殊字符(比如
producción的重音)完全匹配,大小写也要一致。
内容的提问来源于stack exchange,提问作者Samusdr17
相关产品推荐
相关产品推荐

