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

回答編集履歴

3

消し線が動作しないので削除

2021/06/28 14:08

投稿

ppaul
ppaul

スコア24672

answer CHANGED
@@ -31,8 +31,8 @@
31
31
 
32
32
 
33
33
 
34
- 以下は解決積みになった、消しておきます。
34
+ 以下は解決積みになったようです
35
- ~~読めないのはencodingが原因のようなのですが、まだ特定できていません。
35
+ 読めないのはencodingが原因のようなのですが、まだ特定できていません。
36
36
 
37
37
  ```python
38
38
  json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
@@ -52,4 +52,4 @@
52
52
  with open('test.json', encoding='cp932') as f:
53
53
  dct = json.load(f)
54
54
  ```
55
- をやってみてください。どちらかで読み込めるかもしれません。~~
55
+ をやってみてください。どちらかで読み込めるかもしれません。

2

一括設定のコードを追加

2021/06/28 14:08

投稿

ppaul
ppaul

スコア24672

answer CHANGED
@@ -1,6 +1,40 @@
1
- 読めないはencodingが原因のよなのですが、まだ特定できていません
1
+ 辞書データを使って一括設定を行いたいといことです
2
2
 
3
+ 以下の例を参考にしてください。
4
+
3
5
  ```python
6
+ >>> print(dict_input)
7
+ {'style': 2, 'height': 12, 'width': 12, 'title': 'test_散布図', 'x_axis': {'title': 'x座標'}, 'y_axis': {'title': 'y座標'}}
8
+ >>>
9
+ >>> def set_attrs(inst, dct):
10
+ ... for key in dct:
11
+ ... if type(dct[key]) == dict:
12
+ ... set_attrs(inst.__getattribute__(key), dct[key])
13
+ ... else:
14
+ ... inst.__setattr__(key, dct[key])
15
+ ...
16
+ >>> set_attrs(chart, dict_input)
17
+ >>>
18
+ >>> print(chart.style)
19
+ 2
20
+ >>> print(chart.height)
21
+ 12
22
+ >>> print(chart.width)
23
+ 12
24
+ >>> print(chart.title)
25
+ test_散布図
26
+ >>> print(chart.x_axis.title)
27
+ x座標
28
+ >>> print(chart.y_axis.title)
29
+ y座標
30
+ ```
31
+
32
+
33
+
34
+ 以下は解決積みになったので、消しておきます。
35
+ ~~読めないのはencodingが原因のようなのですが、まだ特定できていません。
36
+
37
+ ```python
4
38
  json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
5
39
  ```
6
40
  も出たのですが、そのあといろいろといじったので今再現できません。
@@ -18,4 +52,4 @@
18
52
  with open('test.json', encoding='cp932') as f:
19
53
  dct = json.load(f)
20
54
  ```
21
- をやってみてください。どちらかで読み込めるかもしれません。
55
+ をやってみてください。どちらかで読み込めるかもしれません。~~

1

説明追加

2021/06/28 02:22

投稿

ppaul
ppaul

スコア24672

answer CHANGED
@@ -5,11 +5,14 @@
5
5
  ```
6
6
  も出たのですが、そのあといろいろといじったので今再現できません。
7
7
 
8
+ ファイルをUTF-8Nで保存して以下で読み込む。
8
9
  ```python
9
10
  import json
10
11
  with open('test.json', encoding='utf-8') as f:
11
12
  dct = json.load(f)
12
13
  ```
14
+ というのと、
15
+ ファイルをSJISで保存して以下で読み込む。
13
16
  ```python
14
17
  import json
15
18
  with open('test.json', encoding='cp932') as f: