1
I’m trying to build a table from information collected on a website.
The problem is that although the print comes out as expected, at the time of saving in csv file is going only the last record.
Below the code snippet that’s giving me a headache.
titulo = 0
atributo = 1
alinhado = ""
with open(arquivoOutput, 'w') as csvfile:
escrevelinha = csv.writer(csvfile, delimiter=';', quoting=csv.QUOTE_MINIMAL)
escrevelinha.writerow(["Matricula","Servidor","Cargo","Referência","Remuneração","Abono","Eventuais", "Descontos", "Salario Liquido"])
while (titulo < 18):
apenastag1 = str(apenastag.find_all("td")[titulo].get_text())
apenastag2 = str(apenastag.find_all("td")[atributo].get_text())
#print(apenastag2)
alinhado = alinhado + apenastag2 + ";"
titulo = titulo + 2
atributo = atributo + 2
alinhado = alinhado + "\n"
print(alinhado)
escrevelinha.writerow([alinhado])
The print output is as follows:
The print is going exactly as expected.
Already the CSV file has only the last record.
How do I fix this?
Have you tried using the
write()
instead ofwriterow()
?– Leonardo Pessoa