teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

d

2019/03/02 13:34

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -6,7 +6,6 @@
6
6
  import json
7
7
  import re
8
8
  from collections import OrderedDict
9
- from io import StringIO
10
9
 
11
10
  labels = ['Email', 'Social Networks',
12
11
  'Internet Banking', 'News Sportsites']
@@ -18,9 +17,10 @@
18
17
  for l, v in zip(labels, values):
19
18
  data.append(OrderedDict(axis=l, value=v))
20
19
 
21
- io = StringIO()
22
- json.dump(data, io, indent=2, ensure_ascii=False)
20
+ j = json.dumps(data, indent=2, ensure_ascii=False)
21
+ # 正規表現でキーの引用符を消す。
23
- j = re.sub(r'"(\w+?)":', r'\1:', io.getvalue())
22
+ j = re.sub(r'"(\w+?)":', r'\1:', j)
23
+
24
24
  with open('data.json', 'w') as f:
25
25
  f.write(j)
26
26