user2632088 user2632088 - 4 years ago 263
Python Question

Python find sum of column in table in text file

This is what the file looks like

5.0000E+02 5.23744E-06 0.0006
1.0600E+03 2.15119E-06 0.0023
1.6900E+03 1.83529E-06 0.0035
2.4000E+03 1.76455E-06 0.0044
3.1900E+03 1.78831E-06 0.0050
4.0800E+03 1.86632E-06 0.0056
5.0800E+03 1.91086E-06 0.0061
6.1900E+03 1.97899E-06 0.0066 <--- Get data from here...
7.4300E+03 2.03105E-06 0.0070
8.8400E+03 2.08666E-06 0.0074
1.0400E+04 2.12723E-06 0.0078
1.2200E+04 2.20352E-06 0.0081
1.4100E+04 2.02335E-06 0.0089
1.6400E+04 1.98286E-06 0.0094
1.8900E+04 1.58478E-06 0.0107
2.1700E+04 1.09529E-06 0.0133
2.4900E+04 6.59218E-07 0.0173
2.8500E+04 3.19703E-07 0.0250
3.2500E+04 1.55052E-07 0.0358
3.7000E+04 6.94320E-08 0.0542
4.2100E+04 3.44175E-08 0.0764
4.7800E+04 2.37904E-08 0.0944
5.4200E+04 1.29016E-08 0.1283
6.1400E+04 5.45355E-09 0.1770
6.9500E+04 4.18030E-09 0.2486
7.8700E+04 2.47747E-09 0.2629
8.8900E+04 2.69887E-09 0.2820
1.0100E+05 2.15937E-09 0.4286
1.1300E+05 4.39994E-10 0.7824
1.2800E+05 0.00000E+00 0.0000
1.4400E+05 0.00000E+00 0.0000
1.6300E+05 0.00000E+00 0.0000
1.8300E+05 0.00000E+00 0.0000
2.0700E+05 0.00000E+00 0.0000
2.3300E+05 0.00000E+00 0.0000
2.6300E+05 0.00000E+00 0.0000
2.9600E+05 0.00000E+00 0.0000 <--- ...to here
3.3300E+05 0.00000E+00 0.0000
3.7600E+05 0.00000E+00 0.0000
4.2300E+05 0.00000E+00 0.0000
4.7600E+05 0.00000E+00 0.0000
5.3600E+05 0.00000E+00 0.0000
6.0400E+05 0.00000E+00 0.0000
6.8000E+05 0.00000E+00 0.0000
7.6500E+05 0.00000E+00 0.0000
8.6100E+05 0.00000E+00 0.0000
9.6900E+05 0.00000E+00 0.0000
1.0900E+06 0.00000E+00 0.0000
1.2200E+06 0.00000E+00 0.0000
1.3800E+06 0.00000E+00 0.0000
1.5500E+06 0.00000E+00 0.0000
1.7500E+06 0.00000E+00 0.0000
1.9700E+06 0.00000E+00 0.0000
2.2100E+06 0.00000E+00 0.0000
2.5000E+06 0.00000E+00 0.0000
2.8000E+06 0.00000E+00 0.0000
3.1500E+06 0.00000E+00 0.0000
3.5400E+06 0.00000E+00 0.0000
3.9900E+06 0.00000E+00 0.0000
4.4900E+06 0.00000E+00 0.0000
5.0500E+06 0.00000E+00 0.0000
5.6800E+06 0.00000E+00 0.0000
6.3900E+06 0.00000E+00 0.0000
1.0000E+07 0.00000E+00 0.0000


So the the Python script would get this data:

6.1900E+03 1.97899E-06 0.0066
7.4300E+03 2.03105E-06 0.0070
8.8400E+03 2.08666E-06 0.0074
1.0400E+04 2.12723E-06 0.0078
1.2200E+04 2.20352E-06 0.0081
1.4100E+04 2.02335E-06 0.0089
1.6400E+04 1.98286E-06 0.0094
1.8900E+04 1.58478E-06 0.0107
2.1700E+04 1.09529E-06 0.0133
2.4900E+04 6.59218E-07 0.0173
2.8500E+04 3.19703E-07 0.0250
3.2500E+04 1.55052E-07 0.0358
3.7000E+04 6.94320E-08 0.0542
4.2100E+04 3.44175E-08 0.0764
4.7800E+04 2.37904E-08 0.0944
5.4200E+04 1.29016E-08 0.1283
6.1400E+04 5.45355E-09 0.1770
6.9500E+04 4.18030E-09 0.2486
7.8700E+04 2.47747E-09 0.2629
8.8900E+04 2.69887E-09 0.2820
1.0100E+05 2.15937E-09 0.4286
1.1300E+05 4.39994E-10 0.7824
1.2800E+05 0.00000E+00 0.0000
1.4400E+05 0.00000E+00 0.0000
1.6300E+05 0.00000E+00 0.0000
1.8300E+05 0.00000E+00 0.0000
2.0700E+05 0.00000E+00 0.0000
2.3300E+05 0.00000E+00 0.0000
2.6300E+05 0.00000E+00 0.0000
2.9600E+05 0.00000E+00 0.0000


Then I need the sum of the central column.

Like this:
(1.97899E-06 + 2.03105E-06 + 2.08666E-06 + ... + 0.00000E+00) = 1.90994E-05

Only the second column matters for this problem.

The first column represent time.
The second column represent some data numbers.
The third column represent some random numbers.

please help me to find out :((

Answer Source
import numpy
data = numpy.loadtxt('filename.txt')
print(data[7:,1].sum())

It's possible that I have the indexes transposed, in which case it would be data[1,7:].sum()

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download