Changing the CSV Delimiter in Python

0

I'm having a difficulty in the cell delimiter of the CSV file, when I command the save the edited file in CSV % with% delimiter by default is escritor.writerow([linhas]) , plus I need change to , because when I open the file in Excel , it does not recognize the breaking of the cells with ; , so 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])
    
asked by anonymous 27.03.2018 / 14:35

2 answers

1

I was able to resolve my doubts. Python exported the files without the delimiter I wanted because I had to export as a vector between [] in the above exported code with the , delimiter and not with ; or just took the [] of the parameter

escritor.writerow(linhas)
    
28.03.2018 / 14:04
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( ... )
# ...
    
27.03.2018 / 15:00