将变量分配给文件夹中的所有文件以供访问
我有一个包含“.pkl”文件的文件夹。我想访问这些多个文件中的数据来绘制结果。
当我尝试使用 for 循环执行此操作时出现错误。我的所有 .pkl 文件的文件名中都包含编号,例如 meta_room_1_reg.pkl、meta_room_2_reg.pkl 等。所以我想为每个变量分配一个变量。
目前,我正在使用 if 和 find() 语句来执行此操作。但这并不理想。
我的代码在这里:
all_files = glob.glob('D:/Master Thesis/MCDM Combined code/results/*.pkl')
for i, curr_file in enumerate(all_files):
with open(curr_file, 'rb') as f:
mydata = pickle.load(f)
if curr_file.find('1'):
myfile = mydata
请指导。
I have a folder that contains'.pkl' files. I want to access the data inside those multiple files to plot my results.
I am getting an error when I try to do it with a for loop. All my .pkl files contain numbering in their filename like meta_room_1_reg.pkl, meta_room_2_reg.pkl and so on. So I want to assign a single variable to each and every one.
Currently, I am doing that with an if and find() statement. But that is not ideal.
My code is here:
all_files = glob.glob('D:/Master Thesis/MCDM Combined code/results/*.pkl')
for i, curr_file in enumerate(all_files):
with open(curr_file, 'rb') as f:
mydata = pickle.load(f)
if curr_file.find('1'):
myfile = mydata
Please guide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您想要收集所有数据以列出或类似的东西,对吧?
使用
.append
方法,而不仅仅是赋值。I'm assuming that you want to collect all data to list or something like that, right?
Use the
.append
method, instead of just an assignment.