-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathjson2csv.py
More file actions
78 lines (63 loc) · 1.93 KB
/
Copy pathjson2csv.py
File metadata and controls
78 lines (63 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import sublime, sublime_plugin
import simplejson as json
import csv
import cStringIO
import os
from types import *
class Json2csvCommand(sublime_plugin.TextCommand):
# Get the content of the json file or selection
def get_json_content(self):
regions = self.view.sel()
if len(regions) > 0 and not regions[0].empty():
selection = regions[0]
else:
selection = sublime.Region(0, self.view.size())
return self.view.substr(selection)
# Write data to csv
def write_csv(self, csv_buffer):
write_header = True
item_keys = []
writer = csv.writer(csv_buffer)
for item in self.data:
for key in item:
if not key in item_keys:
item_keys.append(key)
writer.writerow(item_keys)
for item in self.data:
item_values = []
for key in item_keys:
value = item.get(key, '')
if type(value) is StringTypes:
item_values.append(value.encode('utf-8'))
else:
item_values.append(value)
writer.writerow(item_values)
def run(self, edit):
self.data = []
self.source_filename = self.view.file_name()
#print self.source_filename
json_content = self.get_json_content()
# Validate json data
try:
#print "json data is valid"
self.data = json.loads(json_content)
if self.source_filename:
dest_filename = os.path.splitext(self.source_filename)[0] + '.csv'
with open(dest_filename, 'wb') as csv_buffer:
self.write_csv(csv_buffer)
sublime.active_window().open_file(dest_filename)
else:
csv_buffer = cStringIO.StringIO()
self.write_csv(csv_buffer)
window = self.view.window()
csv_file_view = window.new_file()
selection = sublime.Region(0, csv_file_view.size())
#print selection
edit = csv_file_view.begin_edit('json2csv')
csv_file_view.replace(edit, selection, csv_buffer.getvalue())
csv_file_view.end_edit(edit)
except Exception:
import sys
exc = sys.exc_info()[1]
sublime.status_message(str(exc))
#print str(exc)