Create csv table in python

1

I'm trying to build a table from information collected on a website.

The problem is that although print comes out as expected, at the time of saving in csv file only the last record is going.

Below the code snippet that is 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:

Printiscomingoutexactlyasexpected.

TheCSVfilehasonlythelastrecord.

How do I fix this?

    
asked by anonymous 26.11.2017 / 05:56

2 answers

1

Move this line:

escrevelinha.writerow([alinhado])

That is out of while , into it, because so every interaction and search of the element, is already being recorded in csv.

    
27.11.2017 / 02:58
0

Or do so using ILDE-Python on Nosql specialized in CSV called CSV Comp DB:

import cql_to_python
db ="path\dbfolder\"
table = "Mytable"
cql = str("")
cql = create_table(db,table) #cria tablea csv
cql = cql + create_fields(db,table,"(ID ; FIELD1 ; FIELD2)") #cria campos
cql = cql + add_data(db,table,"(0001 ; DATA1 ; DATA2)") #add dados
CSV_COMP_DB_execute(cql) #executa o banco de dados
    
31.12.2017 / 23:48