python - FileNotFoundError:[Errno 2] 没有这样的文件或目录:'corpus_or_AB_FMC.xlsx'
问题描述
我有一个包含很多 excel 文件的目录。我的目标是阅读所有这些 excel 文件并从中提取一些信息。我使用下面的脚本来读取目录,但仍然出现错误。文件已被识别,但代码告诉它没有创建它们,这很奇怪,因为有一行打印文件的名称。但是用pandas读取文件时,是不成立的。
/home/geta/kelo/eXP/Test/corpus
-----File in processed : corpus_or_AB_FMC.xlsx
Traceback (most recent call last):
File "test_vec.py", line 111, in <module>
sentences = pd.read_excel(file, sheet_name= 0)
File "/home/getalp/kelodjoe/anaconda3/lib/python3.7/site-packages/pandas/util/_decorators.py", line 208, in wrapper
return func(*args, **kwargs)
File "/home/geta/kelo/anaconda3/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 310, in read_excel
io = ExcelFile(io, engine=engine)
File "/home/geta/kelo/anaconda3/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 819, in __init__
self._reader = self._engines[engine](self._io)
File "/home/geta/kelo/anaconda3/lib/python3.7/site-packages/pandas/io/excel/_xlrd.py", line 21, in __init__
super().__init__(filepath_or_buffer)
File "/home/geta/kelo/anaconda3/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 359, in __init__
self.book = self.load_workbook(filepath_or_buffer)
File "/home/geta/kelo/anaconda3/lib/python3.7/site-packages/pandas/io/excel/_xlrd.py", line 36, in load_workbook
return open_workbook(filepath_or_buffer)
File "/home/geta/kelo/anaconda3/lib/python3.7/site-packages/xlrd/__init__.py", line 111, in open_workbook
with open(filename, "rb") as f:
这是代码:
dir = "/home/geta/kelo/eXP/Test/corpus"
for root, subdirs, files in os.walk(dir):
print(root)
for file in files:
#print(files)
print("-----File in processed :", file)
# -----File in processed : corpus_or_AB_FMC.xlsx # this file si located in the corpus directory
sentences = pd.read_excel(file, sheet_name= 0)
data_id = sentences.identifiant
print("Total phrases: ", len(data_id))
data = sentences.verbatim
data_label = sentences.etiquette
#print(type(data_id))
#print(type(data))
#number = LabelEncoder()
# 0 = C; 1= F; 2= M
#data_label = number.fit_transform(sentences.etiquette.astype('str'))
#print(data_label)
print("etiquette :" , sentences['etiquette'].unique())
classes = sentences['etiquette'].unique()
len_classes = len(classes)
解决方案
file
是一个文件名,不包括它的路径。使用os.path.join
:
sentences = pd.read_excel(os.path.join(root, file), sheet_name=0)
加入文件名及其绝对路径。
推荐阅读
- mediawiki - 在 Wikidata API 中执行全文搜索时检索 Wikipedia 页面标题
- javascript - 有人玩游戏时如何自动分配角色?V12 不和谐 js
- python - 如何使用 matplot lib 和 python 绘制和保存单独的图
- java - 在 JAVA 中打印数组中的素数
- javascript - 使用 Javascript 动态更改音频源
- mysql - MySQL - 对两个 Select 语句执行减法的正确方法
- c++ - 我正在尝试按降序对地图进行排序,但没有得到想要的结果?
- node.js - 使用 Sequelize 多对多的额外字段条件
- c# - ViewModel 是否必须取消订阅在 ViewModel 中实例化的对象的事件?
- python - Django:操作表单的 csv 文件输入