Changing the Python CSV File Delimiter

Asked

Viewed 1,669 times

0

I have a difficulty in delimiting the cells of the CSV file, when I send the command to save the edited file in CSV escritor.writerow([linhas]) the default delimiter is ,, the more I need to change to ; because when I open the file on Excel, it does not recognize the breaking of cells with ,, Then it’s all in one column. My code is like this:

import csv

coluna_Heading = 0
coluna_Number = 1

with open("testeIfHeading00.csv", "r") as arquivo_lido, open("saida.csv", "w", newline= "") as arquivo_criado:

    #def o leitor CSV do arquivo
    leitor = csv.reader(arquivo_lido, delimiter=';')

    #def o escritor CSV do arquivo
    escritor = csv.writer(arquivo_criado, delimiter=';')

    #percorre as linhas do arquivo a ser lido
    separatec = " "
    for linhas in leitor:
        if (len(linhas[coluna_Number]) == 1) or len(linhas[coluna_Number]) <= 2:
            separatec = linhas[coluna_Heading]
        linhas.append(separatec)

        print(linhas)
        escritor.writerow([linhas])
  • Missing a code snippet

2 answers

1

Use the "delimiter" parameter, for example:

import csv
# ...
with open('arquivo.csv', 'wb') as arquivo:
    arquivo_csv = csv.writer(arquivo, delimiter=';')
    arquivo_csv.writerow( ... )
# ...

1


I was able to solve my doubt. python exported the files without the delimiter I wanted because it had to export as an array between [ ] in the above code exported with the delimiter , and not with ; or just take the [ ] of the parameter

escritor.writerow(linhas)

Browser other questions tagged

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