Python - filtering data in text files

Asked

Viewed 1,515 times

2

I’m trying to write a code to read a . txt file and extract numeric coordinates, but this file contains lines with text as well. I’m unable to filter. see a part of the file.

imagem do arquivo txt So far I’ve managed 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 on line 236 and 105, 100, 35 of the other lines. Since the coordinates x y z are the next step would be to subtract the fifth column from the second - the sixth from the third - and the seventh from the fourth.

  • 1

    Blz, but you failed to explain your doubt, where you need help, what you couldn’t do, and you doubt it. Try to put in and out expected, this helps a lot. If you want to filter only the values of the coordinates on lines 236, 244, 250 and 256 it is quite simple. But I imagine that’s not it

1 answer

2

To filter these values observe the patterns of your file, a quick look gave me some patterns that seem to satisfy your needs, the first column should always be maior que zero and the values contained between the column dois até a nove must not possess zero, we can then multiply all values from column two to the ninth, if the result is greater than zero the row will satisfy your needs, if these two conditions are met save the entire row for later use, follow 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 together your question the input file instead of just putting the image of it...

  • About the expected exceptions, I think this other comment from @jsbueno is relevant to your response

  • in my code note the line map(int, values[0:9]) it converts to whole

  • Yeah, the problem is just the expect empty, which in Python 3 would be syntax error

  • I have no way to test in python3, I only have the python27 and run like a Charm lol, it means that python3 would not leave mine except followed by a continue pass by?

  • Yes. I believe that expect ValueError would solve everything

  • http://ideone.com/, has interpreter for Python3

  • 2

    humm also funfou no python27, interesting Huahua, I put to leave as usable as possible for both versions...

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.