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
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
– helena
@maynahelena, an alternative would be
out_csv.writerow(row[:8] + [row[34]] )
– Cleyton