Python Forum

Full Version: Removing Certain Numbers From File
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey Everyone,

I have a data file full of various numbers e.g.

1 2 3 1 180
2 3 4 1 180
3 4 5 1 180
4 5 6 1 180
5 6 7 1 180
6 7 8 1 180
7 8 9 1 180
8 9 10 1 180
9 10 11 1 180
10 11 12 1 180
12 13 14 1 180
11 12 13 1 180
15 16 17 1 180
14 15 16 1 180
13 14 15 1 180
18 19 20 1 180
17 18 19 1 180
16 17 18 1 180
1000
2 3 4 1 154.251
1 2 3 1 158.186
3 4 5 1 174.914
4 5 6 1 178.98
5 6 7 1 173.529
6 7 8 1 170.674
7 8 9 1 167.303
8 9 10 1 171.968
9 10 11 1 166.438
11 12 13 1 167.12
10 11 12 1 173.609
12 13 14 1 169.945
15 16 17 1 163.017
14 15 16 1 169.985
13 14 15 1 168.141
18 19 20 1 135.138
17 18 19 1 175.661
16 17 18 1 163.794
2000
1 2 3 1 170.264
2 3 4 1 176.227
3 4 5 1 153.696
4 5 6 1 107.968
5 6 7 1 133.734
6 7 8 1 173.96
7 8 9 1 158.41
8 9 10 1 168.659
9 10 11 1 168.945
11 12 13 1 162.38
10 11 12 1 172.85
12 13 14 1 163.664
13 14 15 1 138.511
16 17 18 1 106.912
15 16 17 1 149.535
14 15 16 1 149.883
18 19 20 1 117.114
17 18 19 1 127.023
3000
1 2 3 1 119.887
2 3 4 1 110.368
3 4 5 1 115.445
4 5 6 1 170.901
5 6 7 1 155.545
etc,

As you can see I also have multiple of 1000 in here and this goes on up to 1000000. I want to know if anyone can help me with trying to write a python script that can remove the multiples of 1000 from this text file.
At the moment i've tried the code below but it just produces an empty output file.

infile1 = 'angles1.txt'
outfile1 = 'angles_out.txt'
fin1 = open(infile1)
fout1 = open(outfile1, 'w+')

def filter_by_multiple(seq, n):
for i in seq:
if i % n:
yield i

filter_by_multiple(fout1, 1000)
list(filter_by_multiple(fout1, 1000))

fout1.write(''.join(list(filter_by_multiple(fout1, 1000))))
fin1.close()
fout1.close()

Thanks in advance!
Hi

My advice is by:
  • creating an array with all the numbers
  • using the modulo property (see numpy.mod) by an additional column
  • then looking for values where the rest is equal to zero (see numpy.where)
  • and finally deleting the corresponding rows (see numpy.delete)

Paul
(Feb-07-2020, 03:45 PM)paul18fr Wrote: [ -> ]Hi

My advice is by:
  • creating an array with all the numbers
  • using the modulo property (see numpy.mod) by an additional column
  • then looking for values where the rest is equal to zero (see numpy.where)
  • and finally deleting the corresponding rows (see numpy.delete)

Paul


Hi Paul,

When trying to use numpy.mod I get the following error

TypeError: ufunc 'remainder' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

can you provide any solutions for this? I am new to using python so appreciate any and all help.

Thanks.