【发布时间】:2019-02-02 15:05:59
【问题描述】:
我想创建一个这样的 json 文件:
{"946705035":4,"946706692":4 ...}
我正在获取一个仅包含 Unix Timestamp 的列并将它们分组。
result = data['Last_Modified_Date_unixtimestamp_no_time'].value_counts()
In [21]: result.head()
Out[21]:
1508284800 131
1508716800 106
1508371200 101
1508457600 99
1508630400 96
Name: Last_Modified_Date_unixtimestamp_no_time, dtype: int64
转换成字典
result = result.to_dict()
result
'''
{1507161600: 1,
1507852800: 1,
1508198400: 64,
1508284800: 131,
...
1535155200: 1,
1535241600: 1}
'''
import json
result = json.dumps(result)
with open('result.json', 'w') as fp:
json.dump(result, fp, indent=4)
{"946705035":4,"946706692":4}
【问题讨论】:
-
这看起来是一个合适的解决方案吗? Json dumps escaping forward slashes
标签: python json python-3.x