Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
cotEditorでJSON整形するためのコード。
#!/usr/bin/python
# coding: UTF-8
# %%%{CotEditorXInput=Selection}%%%
# %%%{CotEditorXOutput=ReplaceSelection}%%%
#! /usr/bin/env python
# coding: utf-8
import subprocess
import sys
def getClipboardData():
p = subprocess.Popen(['pbpaste'], stdout=subprocess.PIPE)
retcode = p.wait()
data = p.stdout.read()
return data
def setClipboardData(data):
p = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)
p.stdin.write(data)
p.stdin.close()
retcode = p.wait()
def jsonformat(strings):
tablevel=0
fstr=""
for s in strings:
if s==",":
fstr+=s+"\n"
tab=tablevel*"\t"
fstr+=tab
elif s=="{" or s=="[":
tablevel=tablevel+1
tab=tablevel*"\t"
fstr+="\n"+tab+s+"\n"+tab
elif s=="}" or s=="]":
tab=tablevel*"\t"
fstr+="\n"+tab+s+"\n"
tablevel=tablevel-1
fstr+=tab
elif s=="" or s=="\t"or s=="\n":
pass
else:
fstr+=s
return fstr
def getclip():
text=""
for line in sys.stdin:
text+=line
formatedtext=jsonformat(text)
print formatedtext
def main():
getclip()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.