Search and statistics
Task:Find out the files containing the specified word in all the texts in the directory, and list the contents of the row and row number.
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() |
It seems that it can also be done in the form of apply (lambda:), but it is not as clear and understandable as for loop.
esProc
| A | ||
| 1 | =directory@p(“*.txt”) | |
| 2 | =A1.conj(file(~).read@n().(if(pos(~,"xxx"),[A1.~,#,~].string())).select(~)) |
esProc provides the traversal function of the file system. Combined with the text computing ability, it can be done in two lines of code.
