2025-09-10:仓库迁移
This commit is contained in:
41
4.迭代器与生成器/13.创建处理数据的管道.py
Normal file
41
4.迭代器与生成器/13.创建处理数据的管道.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import os
|
||||
from fnmatch import fnmatchcase
|
||||
|
||||
if __name__ == "__main__":
|
||||
base_path = "D:\Code\Learn\practice"
|
||||
|
||||
# 想要建立一个处理管道,需要将几个生成器函数堆叠,比如,我现在要输出所有practice文件夹下的注释行内容
|
||||
# 1.首先生成文件夹路径
|
||||
def gen_dir_path(fpath):
|
||||
for path, dir, files in os.walk(fpath):
|
||||
for file in files:
|
||||
if (not fnmatchcase(path, "*.[git][idea]*")) and (not fnmatchcase(file, "*.md")):
|
||||
yield os.path.join(path, file)
|
||||
|
||||
# 2.打开文件
|
||||
def gen_file(paths):
|
||||
for path in paths:
|
||||
f = open(path, 'rt', encoding='utf-8')
|
||||
yield f
|
||||
f.close()
|
||||
|
||||
# 3.读取文件内容
|
||||
def gen_file_txt(opened_files):
|
||||
for file in opened_files:
|
||||
yield from file
|
||||
print("done")
|
||||
|
||||
# 4.匹配符合条件的文件行
|
||||
def read_file_lines(file_lines):
|
||||
for line in file_lines:
|
||||
if "#" in line:
|
||||
print(line)
|
||||
|
||||
# 将这些函数堆在一起就组成了一条管道,每次生成一个对象进行流水线处理,
|
||||
# 这样避免了for循环的嵌套
|
||||
paths = gen_dir_path(base_path)
|
||||
files = gen_file(paths)
|
||||
lines = gen_file_txt(files)
|
||||
read_file_lines(lines)
|
||||
|
||||
|
Reference in New Issue
Block a user