Python Forum
Removing Certain Numbers From File
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Removing Certain Numbers From File
#1
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!
Reply
#2
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
Reply
#3
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Removing the unwanted data from a file jehoshua 14 4,061 Feb-01-2022, 09:56 PM
Last Post: jehoshua
  Calcolate the average of numbers from a .txt file francescomiles 2 3,012 Mar-27-2021, 02:43 PM
Last Post: francescomiles
  Read strings and numbers in columns from a file suvadip 4 2,872 Aug-11-2020, 09:37 PM
Last Post: suvadip
  removing duplicate numbers from a list calonia 12 5,239 Jun-16-2019, 12:09 PM
Last Post: DeaD_EyE
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,719 May-09-2019, 12:19 PM
Last Post: Pleiades
  writing numbers to csv file SchroedingersLion 7 4,041 Dec-20-2018, 10:48 AM
Last Post: perfringo
  Help deleting numbers from file names Shane 5 4,843 Feb-04-2018, 10:42 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020