Python 计算一个目录中多个文本文件中的行数并生成简单报告的脚本
Python script counting lines in multiple text files in one directory and producing simple report
我需要一个 python 脚本来计算一个目录中所有文本文件的行数,并生成一份关于文件数量的一般报告,行数为 n。
报告应如下所示:
Files with 1 line: 636
Files with 2 lines: 346
Files with 3 lines: 234
Files with 4 lines: 723
Files with 5 lines: 254
Files with 6 lines: 223
Files with 7 lines: 1464
etc.
我找到了这个用于计算目录中所有文件行数的脚本
:
#!/usr/bin/env python
import csv
import copy
import os
import sys
import glob
#get current working dir, set count, and select file delimiter
os.chdir('/mydirectory')
#parses through files and saves to a dict
names={}
for fn in glob.glob('*.txt'):
with open(fn) as f:
names[fn]=sum(1 for line in f if line.strip() and not line.startswith('#'))
print names
#save the dictionary with key/val pairs to a csv
with open('seriescount.csv', 'wb') as f:
w = csv.DictWriter(f, names.keys())
sum(names.values())
我们如何着手生成像上面那样的简单报告?
谢谢
你的 names
字典看起来像这样:
{
'file1.txt': 30,
'file2.txt': 26,
'file3.txt': 19,
'file4.txt': 19
}
所以你只需要从那里开始然后跟着:
from collections import defaultdict
lines = defaultdict(int)
for val in names.values():
lines[val] += 1
for k, v in lines.items():
print("Files with {} lines: {}".format(k, v))
这将打印如下内容:
Files with 19 lines: 2
Files with 26 lines: 1
Files with 30 lines: 1
我需要一个 python 脚本来计算一个目录中所有文本文件的行数,并生成一份关于文件数量的一般报告,行数为 n。
报告应如下所示:
Files with 1 line: 636
Files with 2 lines: 346
Files with 3 lines: 234
Files with 4 lines: 723
Files with 5 lines: 254
Files with 6 lines: 223
Files with 7 lines: 1464
etc.
我找到了这个用于计算目录中所有文件行数的脚本
#!/usr/bin/env python
import csv
import copy
import os
import sys
import glob
#get current working dir, set count, and select file delimiter
os.chdir('/mydirectory')
#parses through files and saves to a dict
names={}
for fn in glob.glob('*.txt'):
with open(fn) as f:
names[fn]=sum(1 for line in f if line.strip() and not line.startswith('#'))
print names
#save the dictionary with key/val pairs to a csv
with open('seriescount.csv', 'wb') as f:
w = csv.DictWriter(f, names.keys())
sum(names.values())
我们如何着手生成像上面那样的简单报告? 谢谢
你的 names
字典看起来像这样:
{
'file1.txt': 30,
'file2.txt': 26,
'file3.txt': 19,
'file4.txt': 19
}
所以你只需要从那里开始然后跟着:
from collections import defaultdict
lines = defaultdict(int)
for val in names.values():
lines[val] += 1
for k, v in lines.items():
print("Files with {} lines: {}".format(k, v))
这将打印如下内容:
Files with 19 lines: 2
Files with 26 lines: 1
Files with 30 lines: 1