Assuming your data is in a plain text file like this:
$ cat data.txt
0 4028.44 4544434.50 -6.76 -117.00 0.0002 0.12
0 4028.50 3455014.50 -5.86 0 0.0003 0.39
0 7028.56 4523434.50 -4.95 -137.00 0.0005 0.25
0 8828.62 4543414.50 -3.05 0 0.0021 0.61
0 4028.44 4544434.50 -6.76 -107.00 0.0002 0.12
0 4028.50 3455014.50 -5.86 -11.00 0.0003 0.39
0 7028.56 4523434.50 -4.95 -127.00 0.0005 0.25
0 8828.62 4543414.50 -3.05 0 0.0021 0.61
And you are not using any external libraries. The following will read the data into a list
of string
s, omiting the undesirable lines. You can feed these lines into any other function you choose. I call print
merely to demonstrate. N.B: The fifth column has index '4', since list
indices are zero-based.
$ cat data.py
#!/usr/bin/env python
print "1. Delete the rows which have '0' as a value on 5th column:"
def zero_in_fifth(row):
return row.split()[4] == '0'
required_rows = [row for row in open('./data.txt') if not zero_in_fifth(row)]
print ''.join(required_rows)
print '2. Choose the range (i.e. remove the rows which have values between -50 and 30 on 5th column):'
def should_ignore(row):
return -50 <= float(row.split()[4]) <= 30
required_rows = [row for row in open('./data.txt') if not should_ignore(row)]
print ''.join(required_rows)
When you run this you will get:
$ python data.py
1. Delete the rows which have '0' as a value on 5th column:
0 4028.44 4544434.50 -6.76 -117.00 0.0002 0.12
0 7028.56 4523434.50 -4.95 -137.00 0.0005 0.25
0 4028.44 4544434.50 -6.76 -107.00 0.0002 0.12
0 4028.50 3455014.50 -5.86 -11.00 0.0003 0.39
0 7028.56 4523434.50 -4.95 -127.00 0.0005 0.25
2. Choose the range (i.e. remove the rows which have values between -50 and 30 on 5th column):
0 4028.44 4544434.50 -6.76 -117.00 0.0002 0.12
0 7028.56 4523434.50 -4.95 -137.00 0.0005 0.25
0 4028.44 4544434.50 -6.76 -107.00 0.0002 0.12
0 7028.56 4523434.50 -4.95 -127.00 0.0005 0.25