Delete a row and column range in python?

Asked

Viewed 172 times

0

Hello. I want to delete rows and columns from a csv file, and just keep the lines in the 15212 to 21777 range, from a file with more than 23,000 lines. The columns I managed to delete in the way I need. The point is, that in addition to not being able to stay only with this interval, and I end up deleting the head of the tb file.

import csv

in_file = open("Downloads/Dados_PNBoia/B116353.csv", "r")
out_file = open("Downloads/Dados_PNBoia/B116353_transformed.csv", "w", newline="")
in_csv = csv.reader(in_file)
out_csv = csv.writer(out_file)
for row_number, row in enumerate(in_csv):
if row_number  >= 15221:
out_csv.writerow(row[:10])
in_file.close()
out_file.close()

I tried to put the between 0 and 15211, but it was a mistake. P.S.: Just to reinforce, is coming out the name of each column, as 'date', 'lat' etc, and need the header. Thank you

2 answers

0


@maynahelena, good afternoon. All right?

I took the liberty of taking the @Cleyton example to make a little observation, ok?

Cleyton, your example solves the problem, but a detail was missing, because without it, at least for me, an error appears "Valueerror: I/O Operation on closed file". It seems that this error happens because the file is, open and shortly after, is closed. With this, the change I made to test includes the: with: open(...). Right at the end of with: open(...) as csvfile: the open file is closed - Then you enter a for that tries to write to that file that has already been closed. That is, I put the for within the with. Thus:

import csv

in_file = open("Downloads/Dados_PNBoia/B116353.csv", "r")
with open("Downloads/Dados_PNBoia/B116353_transformed.csv", "w", newline="") as csvfile:

    in_csv = csv.reader(in_file)
    out_csv = csv.writer(csvfile)

    for row_number, row in enumerate(in_csv):
        #SE nr linha for igual a 0 (cabeçalho) ou nr linha estiver entre o intervado desejado:
        if row_number == 0 or ( row_number  >= 15221 and row_number  <= 21777 ): 
            out_csv.writerow(row[:10])

in_file.close()
out_file.close()
  • For me, they both worked out. Can you tell if you want to use non succeeding columns, type, columns 1,2,3,4,5,6,7,8,34; how should I replace in line 'out_csv.writerow(Row[:10])', would it be 'out_csv.writerow(Row[1,2,3,4,5,6,7,8,34])'? It makes a mistake when I do it. Hugs

  • @maynahelena, an alternative would be out_csv.writerow(row[:8] + [row[34]] )

0

You can use the keyword "OR" to filter the lines in the IF:

IF row nr is equal to 0 (header) OR row nr is between the desired range:

import csv

in_file = open("Downloads/Dados_PNBoia/B116353.csv", "r")
out_file = open("Downloads/Dados_PNBoia/B116353_transformed.csv", "w", newline="")
in_csv = csv.reader(in_file)
out_csv = csv.writer(out_file)

for row_number, row in enumerate(in_csv):

  #SE nr linha for igual a 0 (cabeçalho) ou nr linha estiver entre o intervado desejado:
  if row_number == 0 or ( row_number  >= 15221 and row_number  <= 21777 ): 
    out_csv.writerow(row[:10])
    in_file.close()
    out_file.close()

Browser other questions tagged

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