-
Notifications
You must be signed in to change notification settings - Fork 3
/
txtstats
executable file
·43 lines (40 loc) · 1.27 KB
/
txtstats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
################################################################################
# txtstats - a quick and dirty tool to print some statistics about the given
# text, either from file(s) or from stdin. The format looks something like this:
#
# values: 100
# sum: 6490.44
# min: 44.54
# max: 111.89
# mean: 64.9044
# stddev: 10.4792
#
# Version 2
# Matthew Malensek <[email protected]>
################################################################################
def printstats(fname, printname=False):
import numpy as np
data = np.loadtxt(fname=fname)
strformat = """%s
values: %d
sum: %g
min: %g
max: %g
mean: %g
stddev: %g\n"""
print(strformat % (fname if printname is True else "",
len(data), data.sum(), data.min(), data.max(), data.mean(), data.std()))
if __name__ == "__main__":
import sys
nargs = len(sys.argv)
if nargs > 1:
for fname in sys.argv[1:]:
try:
printstats(fname, printname=(nargs > 2))
except Exception as e:
print("Error reading '%s':\n%s" % (fname, e))
elif sys.stdin.isatty() == False:
printstats(sys.stdin)
else:
print("Usage: %s file1 [file2 ...]" % (sys.argv[0]))