如何将Tinder data.json转换为CSV

时间:2019-08-27 01:25:05

标签: 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

3 个答案:

答案 0 :(得分:2)

要打动您的约会,您显然需要比Python更强悍的工具。由于输入格式为json,因此jq是一个不错的选择:

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

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

答案 1 :(得分:1)

这是一个易于阅读且功能强大的jq程序,可以完成此任务:

["date", "messages_sent", "matches", "messages_received", "swipes_passes", "swipes_likes", "app_opens"] as $keys
| .Usage 
| ([.[] | keys_unsorted] | add | unique) as $dates
| $keys,
  ($dates[] as $d | [$d] + [ .[$keys[1:][]][$d] ])
|  @csv

从几种意义上讲,它是“稳健的”-输入对象中键的顺序并不重要,并且以日期作为键的对象不必具有相同的键,并且它们不必具有相同的顺序,或者确实按时间顺序排列。在所有具有相同或更高健壮性的程序中,该程序也非常有效。

答案 2 :(得分:-1)

此Python代码将完成这项工作:

from __future__ import print_function
import json
import itertools

# load json into an object
with open('data.json') as f:
  d = json.load(f)
usage = d['Usage']

# get all listed dates
dates = sorted(set(itertools.chain.from_iterable([[day for day in usage[t]] for t in usage])))

# pivot data into one row per date with multiple columns
print(','.join(['date']+[t for t in usage]))
for day in dates:
  print(','.join([day] + [str(usage[t][day]) for t in usage]))

这会将json文件中的使用情况数据转换为如下所示的csv:

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
2018-06-06,16,1,9,4,32,2