如何从不同的路径文件夹中读取 csv 文件,并为 Python 中的每个数据集附加配置文件?
How to read csv files from different paths folders, with configuration files attached for each dataset in Python?
到目前为止,我一直在 R 中工作。我正在尝试在 python 中复制我的作品。
因此,在 R 中,我已经能够将不同路径文件夹中的不同数据集读取到列表列表中。在此列表中,我应用了一个函数,该函数使用存储在存储数据的同一列表中的配置文件对数据进行了标准化。但现在我只是想将数据和配置文件读入 python 中的列表列表,不知道如何?有人可以帮忙吗?
这是我在 R 中所做的,也是我读取数据的方式。
import_list <- list(list(data_path = "../data/A.csv",
config_path = "/data/config/A/"),
list(data_path = "../data/B.csv",
config_path = "/data/config/B/"),
list(data_path = "../data/C.csv",
config_path = "../data/C/")))
我希望我能以这种格式读取我拥有的数据,但这次是 python。有没有一种简单的方法可以将不同路径文件夹中的多个 csv 数据文件读入这种格式?它应该是这样的。
import_list List of 3
:List of 2
data_path : chr "../data/A.csv
config_path: chr "config/A"
:List of 2
data_path: chr "../data/B.csv
config_path: chr "config/B"
这可能就足够了:
In [1]: from os import listdir
In [2]: from os.path import isfile, join
In [3]: from re import sub
In [4]: mypath = "test"
In [5]: onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath, f)) ]
In [6]: configs = [ join(mypath, "config", sub("\.csv$", "", f)) for f in onlyfiles ]
In [7]: list(zip(onlyfiles, configs))
Out[7]: [('B.csv', 'test/config/B'), ('A.csv', 'test/config/A')]
到目前为止,我一直在 R 中工作。我正在尝试在 python 中复制我的作品。 因此,在 R 中,我已经能够将不同路径文件夹中的不同数据集读取到列表列表中。在此列表中,我应用了一个函数,该函数使用存储在存储数据的同一列表中的配置文件对数据进行了标准化。但现在我只是想将数据和配置文件读入 python 中的列表列表,不知道如何?有人可以帮忙吗?
这是我在 R 中所做的,也是我读取数据的方式。
import_list <- list(list(data_path = "../data/A.csv",
config_path = "/data/config/A/"),
list(data_path = "../data/B.csv",
config_path = "/data/config/B/"),
list(data_path = "../data/C.csv",
config_path = "../data/C/")))
我希望我能以这种格式读取我拥有的数据,但这次是 python。有没有一种简单的方法可以将不同路径文件夹中的多个 csv 数据文件读入这种格式?它应该是这样的。
import_list List of 3
:List of 2
data_path : chr "../data/A.csv
config_path: chr "config/A"
:List of 2
data_path: chr "../data/B.csv
config_path: chr "config/B"
这可能就足够了:
In [1]: from os import listdir
In [2]: from os.path import isfile, join
In [3]: from re import sub
In [4]: mypath = "test"
In [5]: onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath, f)) ]
In [6]: configs = [ join(mypath, "config", sub("\.csv$", "", f)) for f in onlyfiles ]
In [7]: list(zip(onlyfiles, configs))
Out[7]: [('B.csv', 'test/config/B'), ('A.csv', 'test/config/A')]