Last active
August 29, 2015 14:02
-
-
Save mutsune/2cc9bbe1fa010e733004 to your computer and use it in GitHub Desktop.
CSV から JSON への変換スクリプト (単純な Array か Object への変換)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
import sys | |
indent = [" ", " "] | |
def t(n): | |
return "".join(indent[:n]) | |
l = "\"" | |
p = "[" | |
q = "]\n" | |
p_ = "{\n" | |
_q = "},\n" | |
n = ",\n" | |
def toArray(values): | |
array = "" | |
array += t(1) + p | |
for v in values: | |
array += t(2) + v + n | |
array += t(1) + q | |
return array | |
def toObject(values, labels): | |
obj = "" | |
obj += t(1) + p_ | |
for i in xrange(len(labels)): | |
obj += t(2) + l + labels[i] + l + ": " + l + values[i] + l + n | |
obj += t(1) + _q | |
return obj | |
if __name__ == '__main__': | |
labels = ("a", "b", "c") | |
print "[" | |
for line in sys.stdin: | |
values = line[:-1].split(",") | |
print toObject(values, labels), | |
print "]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment