Saving csv file using python

Asked

Viewed 106 times

-1

I have the following problem:

I have a csv file with multiple numbers.

I read this file, pass a header and make the separation The problem happens when I try to save this data in a new csv file.

Separation does not occur and ends up being equal to the previous file before editing

Follow the code I made:

import pandas as pd

uri = "dados.csv"

cabecalho = ['Axis1', 'Axis2', 'Axis3', 'Steps', 'Lux', 'Inclinometer Off', 'Inclinometer Standing', 'Inclinometer Sitting', 'Inclinometer Lying']


dados =  pd.read_csv(uri, sep=',', header=None, names=cabecalho, index_col=0)
dados.to_csv('dadosLimpos.csv', sep=',')

Example of the CSV:

489,768,860,3,28,0,10,0,0
644,796,684,7,56,0,10,0,0
1219,1294,1721,7,74,0,10,0,0
495,770,556,3,22,0,10,0,0
74,241,386,2,0,0,10,0,0
1153,781,837,7,63,0,10,0,0
191,365,382,2,55,0,10,0,0
27,733,185,1,73,3,0,0,7
7,554,114,1,98,8,0,0,2
213,974,574,4,96,0,10,0,0
  • The intention was to create the header in the new file and keep the numbers?

  • Yes. The header comes normal, but it does not separate the correct numbers in each column. Everyone is saved in "Axis1".

  • You put in the CSV you’re using, I ran a test and it was all right, I didn’t change anything in your code. If the CSV is too large, just put a stretch, a few lines.

  • I edited the question with some csv data

  • Is this the expected result? https://i.stack.Imgur.com/j8U71.png

  • It’s in that shape.

  • So, I didn’t change anything... If you are opening this generated CSV where? In Excel?

  • Yes. He saves with that new name and I open it

  • Maick, this is just Excel itself, it is interpreting the CSV with the delimiter being semicolon, I believe it is the default in the installation BR. Unfortunately at the moment I don’t have an Excel to guide you how to exchange this, but if you want to do a test, change the comma of csv by semicolon and open again.

  • I had done this test. I replaced all the commas with ; . The header it separated, column by column, minus the numbers. I will try to see his settings here. Thank you very much

Show 5 more comments

2 answers

0

The code works as desired, the problem must be the excel delimiter setting (Uses ;).

You can open the file in a text editor and use the find and replace option.

Substitute , for ;

So excel should open the file properly

For another program like Libreoffice it is not necessary to do anything

0

You can write the output file directly into the format .xlsx.

First install the module openpyxl:

$ pip3 install openpyxl

Now, use the following code:

import pandas as pd

entrada = 'dados.csv'
saida = 'dados_limpos.xlsx'
planilha = 'Planilha1'

cabecalho = ['Axis1', 'Axis2', 'Axis3', 'Steps','Lux', 'Inclinometer Off',
    'Inclinometer Standing','Inclinometer Sitting', 'Inclinometer Lying']

df = pd.read_csv(entrada, sep=',', header=None, names=cabecalho, index_col=0)

gravador = pd.ExcelWriter(saida)
df.to_excel(gravador, planilha)
gravador.save()

Output File:

inserir a descrição da imagem aqui

Browser other questions tagged

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