善用系统的stdin,stdout有时可以做出强大的工具。下面的例子是使用python读取stdin的json文件,然后格式化,再写至stdout。
#!/usr/bin/python3
import json
import sys
import os
content = sys.stdin.read()
js = json.loads(content)
js_str = json.dumps(js, indent=4, ensure_ascii=False)
sys.stdout.write(js_str)
此时,就能在本地生成一个文件,是不是很灵活?
cat fmtjson.json | ./fmtjson.py > hello.json