如何自动过滤 python 中的文件?
How to automate filtering files in python?
我有一个文件夹 Measurement,其中包含许多文件(file1data.csv,file2data.csv......,file10data.csv) .csv 格式。
我现在使用的不同功能:
1.readfiles(Path) ---read the files from the folder
2.initialization(Data) -- some initailization
3.Data_to_analyze() ---some specific data for the filtering
4.create_subset() - making subset of the data to analyse
5.filter_file1() - function which contain filtering conditions for file 1
6.filter_file2() - function which contain filtering conditions for file 2
7.filter_file3() - function which contain filtering conditions for file 3
函数5,6,7想在函数内部调用4.Currenlty我在注释函数5的时候需要过滤文件2
函数3,4对所有文件通用
我想自动执行此过程:
函数1中,读取文件1时需要执行函数2-->3-->4-->5.
同file2data,需要执行2-->3-->4-->6 .
如何实现自动化?
目前节目编排如下:
def readfiles(Path):
Filenames = glob.glob(Path + '/*.csv')
for Filename in Filenames:
#read file
initialization(Data)
def initialization(Data):
Data_to_analyze()
def Data_to_analyze():
.
.
create_subset()
def create_subset():
.
.
.
filter_file1()
#filter_file2()
def filter_file1()
.
#filtering conditions
.
.
def filter_file2()
.
#filtering conditions
.
.
我想改进上面的代码。
因为我是初学者,这会很有帮助
我猜你对 10 个文件有 10 个过滤函数。如果是,这个答案对你有用。
def readfiles(Path):
Filenames = glob.glob(Path + '/*.csv')
for count, Filename in enumerate(Filenames, 1):
#read file
function_no = count
initialization(Data, function_no)
def initialization(Data, function_no):
Data_to_analyze()
create_subset()
function_map.get(function_no)()
def Data_to_analyze():
.
.
def create_subset():
.
.
.
#filter_file2()
def filter_file1()
.
#filtering conditions
.
.
final_file2df.to_excel('new_file_path', ignore_index=True)
function_map = {
1: filter_file1
2: filter_file2
3: filter_file3
# please continue the same for remain...
}
我有一个文件夹 Measurement,其中包含许多文件(file1data.csv,file2data.csv......,file10data.csv) .csv 格式。 我现在使用的不同功能:
1.readfiles(Path) ---read the files from the folder
2.initialization(Data) -- some initailization
3.Data_to_analyze() ---some specific data for the filtering
4.create_subset() - making subset of the data to analyse
5.filter_file1() - function which contain filtering conditions for file 1
6.filter_file2() - function which contain filtering conditions for file 2
7.filter_file3() - function which contain filtering conditions for file 3
函数5,6,7想在函数内部调用4.Currenlty我在注释函数5的时候需要过滤文件2
函数3,4对所有文件通用
我想自动执行此过程:
函数1中,读取文件1时需要执行函数2-->3-->4-->5.
同file2data,需要执行2-->3-->4-->6 .
如何实现自动化?
目前节目编排如下:
def readfiles(Path):
Filenames = glob.glob(Path + '/*.csv')
for Filename in Filenames:
#read file
initialization(Data)
def initialization(Data):
Data_to_analyze()
def Data_to_analyze():
.
.
create_subset()
def create_subset():
.
.
.
filter_file1()
#filter_file2()
def filter_file1()
.
#filtering conditions
.
.
def filter_file2()
.
#filtering conditions
.
.
我想改进上面的代码。
因为我是初学者,这会很有帮助
我猜你对 10 个文件有 10 个过滤函数。如果是,这个答案对你有用。
def readfiles(Path):
Filenames = glob.glob(Path + '/*.csv')
for count, Filename in enumerate(Filenames, 1):
#read file
function_no = count
initialization(Data, function_no)
def initialization(Data, function_no):
Data_to_analyze()
create_subset()
function_map.get(function_no)()
def Data_to_analyze():
.
.
def create_subset():
.
.
.
#filter_file2()
def filter_file1()
.
#filtering conditions
.
.
final_file2df.to_excel('new_file_path', ignore_index=True)
function_map = {
1: filter_file1
2: filter_file2
3: filter_file3
# please continue the same for remain...
}