Kelo
Kelo

Reputation: 1893

Converting data to a CSV file

I'm bringing in data that is json.dumps'd to go through the MQTT broker. After I do a json.loads, I'm getting this data in return.

[['date here', '1713180'], ['date here', '1713181'], ['date here', '1713182'], ['date here', '1713183'], ['date here', '1713184']]

I've tried numerous other threads on csv writing, but I'm completely lost as to how to get this written properly to a CSV.

Upvotes: 1

Views: 130

Answers (2)

razimbres
razimbres

Reputation: 5015

Simply:

import pandas as pd
df=pd.DataFrame([['date here', '1713180'], ['date here', '1713181'], ['date here', '1713182'], ['date here', '1713183'], ['date here', '1713184']])
df.columns=['timestamp','data']
df.to_csv('test.csv',columns=['timestamp','data'],index=False)

Upvotes: 1

shayelk
shayelk

Reputation: 1646

"You can use pandas:

if you don't have pandas installed run the following in order to install it:

pip install pandas

then, you can use:

import pandas as pd

data = [['date here', '1713180'], ['date here', '1713181'], ['date here', '1713182'], ['date here', '1713183'], ['date here', '1713184']]
df = pd.DataFrame(data)

df.to_csv("path/to/file.csv")

Upvotes: 1

Related Questions