Find a value in a given column in a.txt file

Asked

Viewed 86 times

0

I am trying to search for a value at a certain position in the column of a text file and generating another file with these lines, but without success

Follows the code:

arquivo = open('arquivo.txt', 'r')
arquivo2 = open('arquivo2.txt', 'w')

for i in arquivo.readlines():
    if i[70:71] == '02':
        arquivo2.write(i)
        print(i.rstrip())



arquivo.close()
arquivo2.close()

1 answer

1


Have you said what happens when you run, if you make a mistake, or if you have some other problem in the result.

Since we don’t have the file, just you, there’s no way to guess what’s going on.

One of the mistakes you can already see is that you make the comparison i[70:71] == '02' however i[70:71] possesses only 1 character so it can never be equal to '02'.

Maybe you want to say i[70:72] or i[69:71] because these have 2 characters.

  • It really was just that detail in the column position! And it did not generate any error, only the empty file, but it worked out by adjusting the position. Thank you!

Browser other questions tagged

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