Exports data to excel with python

Asked

Viewed 312 times

-1

I’m having a problem in python code, which I’m wanting to import some data that is taken from a website and it matters either for excel or for csv.

as you can see in the photo, this is the data, many columns and rows. inserir a descrição da imagem aqui

I managed to solve the problem to do that way

for key, value in result.items():
   df = pd.DataFrame(result_format.format(key,
                               value['Cotacao'],
                               value['P/L'],
                               value['P/VP'],
                               ...
                               ))
   df.to_csv('Dados.csv')

however only one row of this data is taken and all the data is in only the first column of the first row.

And what I wanted to sort out was that each piece of information in the row should be separated by column, how can I solve this.

1 answer

0


Your call result_format.format(key, value['Cotacao'], value['P/L'], value['P/VP'], ... )

does exactly what you are asking - takes all your data, which usually comes separately, and pastes all together in a single string.

And you, for each line read from the database, create a new dataframe , and a NEW CSV file, with the same name.

You show the printed data, but do not show the data structure, nor the code for printing - in case I still had to infer that result_format is a string with the template for printing.

So it is very difficult to give an exact answer - but you must achieve something (possibly "turned 90 degrees"), if you simply pass this yours result how it is in the call to the dataframe:

df = pd.DataFrame(result)
df.to_csv('dados.csv')
  • It worked great, thank you!

Browser other questions tagged

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