首页 > 解决方案 > 将数据转换为 CSV 文件

问题描述

我正在引入 json.dumps 的数据以通过 MQTT 代理。在我做一个 json.loads 之后,我得到了这些数据作为回报。

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

我在 csv 写入方面尝试了许多其他线程,但我完全不知道如何将其正确写入 CSV。

标签: python-3.xcsv

解决方案


“你可以使用熊猫:

如果您没有安装 pandas,请运行以下命令来安装它:

pip install pandas

然后,您可以使用:

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")

推荐阅读