python json转csv_jsoncsv

68747470733a2f2f696d672e736869656c64732e696f2f707970692f762f6a736f6e6373762e737667

jsoncsv is a command tool to convert json file to csv or xlsx file.

It has not ready to be use as a module. :(

command line example :

$cat raw.json|jsoncsv|mkexcel > output.csv

$cat raw.json|jsoncsv|mkexcel -t xls > output.xls

$jsoncsv raw.json expand.json

$mkexcel expand.json -o output.csv

$mkexcel expand.json -t xls -o output.csv

jsoncsv

example:

$echo '{"s":[1,2,{"w":1}]}'|jsoncsv

{"s.2.w": 1,"s.0": 1,"s.1": 2}

-e,–expand

expand json, 展开 json

jsoncsv -e raw.json expand.json

cat raw.json expand.json

{"s":[1,2,{"w":1}]}

{"s.2.w": 1,"s.0": 1,"s.1": 2}

{“s”:[1,2,{“w”:1}]} transformed to {“s.2.w”: 1,”s.0”: 1,”s.1”: 2}

expand.json is only one layer json, it can be easy change to csv or xlsx

-r,–restore

restore 重构被展开的json

jsoncsv -r expand.json raw.json

cat expand.json raw.json

{"s.2.w": 1,"s.0": 1,"s.1": 2}

{"s": [1, 2, {"w": 1}]}

{“s.2.w”: 1,”s.0”: 1,”s.1”: 2} change to {“s”:[1,2,{“w”:1}]}

-s,–separator

separator used for combine the keys in the tree

default separator is .

mkexcel

dump expanded (by jsoncsv) json file to csv or xls

将**jsoncsv** expand 的 json文件转成 csv/xls

mkexcel expand.json -o output.csv

cat expand.json|mkexcel > output.csv

cat expnad.json|mkexcel -t xls > output.xls

-t,–type

chose dump type in [‘csv’, ‘xls’]

cat expand.json|mkexcel -t csv > output.csv

cat expand.json|mkexcel -t xls > output.xls

NOTE

1. key in origin json can’t contain separator

原始json 的 各级key不能包含分隔符 “.”,因为`.`是expand后key的连接字符。

下个版本会考虑这个问题,可能会使用转义

s.w.www.xxx.com –>s.w.www.xxx.com

2. key can’t be all intenger string

字典key中不能混杂数字。如果全部的key都是数字,恢复重构时会被当成list类型。

example:

echo '{"0":1,"1":[1,2]}'|jsoncsv -e| jsoncsv -r

[1, [1, 2]]

实现方案会过于复杂,不想考虑在expand的json中添加类型信息

3. write in xlsx is always str

下个版本会考虑

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值