Python - filtering data into text files

2

I'm trying to write a code to read a .txt file and extract numeric coordinates, except that this file contains lines with text as well. I can not do the filter. see a part of the file.

So far I have been able to write the following:

filename = 'arquivo.txt'
with open(file_name) as f:
    lines = f.readlines()

for line in lines:
values = line.split()

Now I try to filter only the values of the coordinates of lines 236, 244, 250, 256 ... could use the value of the first column to filter because it is standard, for example: 110 at line 236 and 105, 100, 35 of the other lines. Since x and y are coordinates, the next step would be to subtract the fifth column from the second - the sixth from the third - and the seventh from the fourth.     

asked by anonymous 06.06.2017 / 23:56

1 answer

1

To filter these values by looking at your file patterns, a quick glance gave me some patterns that seem to meet your needs, the first column should always be maior que zero , and the values contained in the dois até a nove column should not have zero , we can then multiply all the values of column two until the ninth, if the result is greater than zero the line will satisfy your needs, if these two conditions are met save the entire line for later use, follows code containing the logic explained above:

filename = 'arquivo.txt'

resultado=[]

with open(filename) as f:
    lines = f.readlines()

for line in lines:
    values = line.split()

    try:
        inteiros = map(int, values[0:9])

    except ValueError:
        continue

    try:

        if inteiros[0] > 0:
            multiplica = reduce(lambda x, y: x*y, inteiros[1:8])
            if multiplica > 0:
                resultado.append(values)


    except:
        continue

print resultado

It would help a lot if you had put the input file next to your question instead of just putting the image of it ...

    
07.06.2017 / 02:26