遍历文件夹中的文件以创建numpy数组 [英] Iterate over files in a folder to create numpy array

查看:84
本文介绍了遍历文件夹中的文件以创建numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一篇文章,我真的是编程新手-
我有一个文件夹,其中包含一些要处理的文件,然后创建一个具有我需要的值的numpy数组:

this is my first posting and I am really new to programming - I have a folder with some files that I want to process and then create a numpy array with the values I need I do:

listing = os.listdir(datapath)
my_array=np.zeros(shape=(0,5))
for infile in listing:
    dataset = open(infile).readlines()[1:]
    data = np.genfromtxt(dataset, usecols=(1,6,7,8,9))
    new_array = np.vstack((my_array, data))

虽然我有2个文件在清单(数据路径文件夹)中,new_array数组会覆盖数据,并且仅向我提供第二个文件
的值有什么想法?
谢谢,

and although I have 2 files in listing (datapath folder) the new_array array overwrites the data and gives me only the values of the second file any ideas? thanks,

推荐答案

这是您需要执行的操作,以从特定文件夹中读取numpy数组中的所有文件。我有一个文件夹 test 仅包含 .txt 文件。我下面的 file.py 和所有 .txt都位于同一个 test 文件夹中code>文件。每个 .txt 文件都包含一个4x4矩阵/数组。运行脚本后,获得的矩阵将是[Nx4x4]的numpy数组。

Here is what you need to do to read all files in a numpy array from a specific folder. I have a folder test containing only .txt files. My following file.py is in the same test folder along with all .txt files. Each .txt file contains a 4x4 matrix/array. After running the script the obtained matrices will be a numpy array of [Nx4x4].

import numpy as np
from glob import glob

def read_all_files():
   file_names = glob('test/*')
   arrays = [np.loadtxt(f) for f in file_names]
   matrices = np.concatenate(arrays)

这篇关于遍历文件夹中的文件以创建numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆