How to open a csv created in Python without opening an import screen?

Asked

Viewed 130 times

1

I have created a Python csv in several ways as can be seen below, but every time I open the file opens the import screen as below.

Why does this happen?

And how can I make the file open directly, within csv standards?

Detail: I’m using Libre Office to open the file.

Code 1:

import csv
csvfile = "arquivo.csv"
f=open(csvfile,'wb') # abre o arquivo para escrita apagando o conteúdo
csv.writer(f, delimiter =' ',quotechar =',',quoting=csv.QUOTE_MINIMAL)

Code 2:

import csv

c = csv.writer(open("teste.csv", "w"))

Code 3:

Arquivo = open(Diretorio + "relatorios/" + nome_arquivo_csv, "a")
            Arquivo.write('\n'  +valores_str + '\t' + medidas_str)
            Arquivo.close()

inserir a descrição da imagem aqui

  • I don’t think it’s related to Python, but to Libreoffice

  • 2

    Good thing it opens, otherwise there was no way to know which character(s) column splitter

1 answer

3


Filipe, as commented above, I don’t think it’s a problem related to python or how you generate the file, but to Libreoffice.

Anyway a possible solution for this, would be to save its contents in one of the formats that Libreoffice supports without the need of previous formatting, as for example xlsx.

In a brief search, I found this example, using the library pandas. I tested with python 3.6 and it worked.

 import pandas

 data = pandas.read_csv('example.csv')
 writer = pandas.ExcelWriter('example.xlsx')
 to_excel = data.to_excel(writer, index=False)
 writer.save()

For this example, I used the following CSV.

id,first_name,last_name,email,gender,ip_address
1,Aube,Pynner,[email protected],Male,202.161.177.42
2,Em,McFaul,[email protected],Male,16.179.223.5
3,Nico,Belford,[email protected],Male,239.143.91.73

I hope I’ve helped.

Att,

Browser other questions tagged

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