【对比Python】查找统计
任务:在目录下所有文本中找出含有指定单词的文件,并列出所在行内容及行号
Python
| 1 | import glob |
| 2 | list1=[] |
| 3 | for filename in glob.glob('D:/*.txt'): |
| 4 | file = open(filename,'r') |
| 5 | for num,value in enumerate(file): |
| 6 | if value.find('XXX')>=0: |
| 7 | list1.append(filename+' '+num+' '+value) |
| 8 | file.close() |
Pandas使用apply(lambda:)形式貌似也可以完成,但没有for循环清晰好理解。
集算器
| A | |
| 1 | =directory@p(“*.txt”) |
| 2 | =A1.conj(file(~).read@n().(if(pos(~,"xxx"),[A1.~,#,~].string())).select(~)) |
集算器提供了文件系统的遍历功能,结合文本计算能力,只要两句代码就能完成。
