python - 如何将 Tinder data.json 转换为 CSV

标签 python json export-to-csv jq tinder

Tinder 允许您导出数据 ( https://account.gotinder.com/data ),最终导出一个 data.json 文件。

如何将此嵌套 json 转换为可以加载到电子表格中的 CSV 文件?

该文件看起来像这样:

$ cat data.json  |jq .Usage
{
  "app_opens": {
    "2018-06-03": 3,
    "2018-06-04": 10,
    "2018-06-05": 2,
...

用途:

messages_sent
matches
messages_received
swipes_passes
swipes_likes
app_opens

包含有趣数据的完整 json 如下所示:

{
    "Usage": {
        "app_opens": {
            "2018-06-03": 3,
            "2018-06-04": 10,
            "2018-06-05": 2
        },
        "messages_sent": {
            "2018-06-03": 7,
            "2018-06-04": 9,
            "2018-06-05": 0
        },
        "matches": {
            "2018-06-03": 3,
            "2018-06-04": 1,
            "2018-06-05": 7
        },
        "messages_received": {
            "2018-06-03": 30,
            "2018-06-04": 1,
            "2018-06-05": 20
        },
        "swipes_passes": {
            "2018-06-03": 56,
            "2018-06-04": 1,
            "2018-06-05": 8
        },
        "swipes_likes": {
            "2018-06-03": 30,
            "2018-06-04": 4,
            "2018-06-05": 4
        }
    }
}

输出应如下所示:

date,messages_sent,matches,messages_received,swipes_passes,swipes_likes,app_opens
2018-06-03,0,2,0,4,10,2
2018-06-04,2,2,1,1,18,6
2018-06-05,35,7,32,1,47,3

最佳答案

为了给你的约会对象留下深刻的印象,你显然需要比 Python 更黑客的东西。 jq 是一个不错的选择,因为输入格式是 json:

tndr2csv

#!/usr/bin/jq -rf
.Usage as $u|$u|keys as $k|
(["date"]+$k|@csv),
(.[$k[0]]|keys|map(. as $d|[.]+($k|map($u[.][$d]))|@csv))[]

像这样运行:

$ chmod +x tndr2csv
$ ./tndr2csv data.json

输出:

"date","app_opens","matches","messages_received","messages_sent","swipes_likes","swipes_passes"
"2018-06-03",3,3,30,7,30,56
"2018-06-04",10,1,1,9,4,1
"2018-06-05",2,7,20,0,4,8

...可以作为电子表格打开。

关于python - 如何将 Tinder data.json 转换为 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57666633/

相关文章:

Python - 缩进返回语句

javascript - 将 dojo 数据网格导出到 CSV 文件

mysql - 使用python从mysql字符串中搜索关键字?

python - 使用 Zeroconf/PyBonjour 在 Python 中读/写字符串

Python - 我的 MySQL 查询中的错误在哪里?

python - 如何在循环中无延迟地刷新 Tkinter 显示

python - pd.read_csv 解析不正确

javascript - jQuery ajax 无法检索 json

mysql - 数据库json列在laravel Blade中返回转义字符串

objective-c - 访问未知字典键的值