回答編集履歴
5
追記
test
CHANGED
@@ -79,3 +79,41 @@
|
|
79
79
|
|
80
80
|
|
81
81
|
もしファイルの生成・読み取りの自動化を狙っているのなら、こちらの方が良いでしょう。
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
今更だけど
|
86
|
+
|
87
|
+
---
|
88
|
+
|
89
|
+
ちょっとだけシンプルにリライトしました。[Wandbox](https://wandbox.org/permlink/71e9UqyPjBAxVy3Q)
|
90
|
+
|
91
|
+
```Python
|
92
|
+
|
93
|
+
import re
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
pattern = re.compile(r"""([\w|'|,]+)""")
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
data_dict = {}
|
102
|
+
|
103
|
+
with open('file.txt') as f:
|
104
|
+
|
105
|
+
for row in f:
|
106
|
+
|
107
|
+
find_obj = re.findall(pattern, row)[0]
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
key, value = map(lambda x: x.strip("(')"), find_obj.split(','))
|
112
|
+
|
113
|
+
data_dict[key] = value
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
print(data_dict)
|
118
|
+
|
119
|
+
```
|
4
自信ないので
test
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
無理矢理書いてみました。 [Wandbox](https://wandbox.org/permlink/n3IcJA8Ud06jmsoP)
|
2
|
+
|
3
|
+
正規表現に苦手意識があるので、無駄な処理をしてしまっている気がしますが。
|
2
4
|
|
3
5
|
```Python
|
4
6
|
|
3
修正
test
CHANGED
@@ -32,7 +32,7 @@
|
|
32
32
|
|
33
33
|
|
34
34
|
|
35
|
-
`file.
|
35
|
+
`file.txt`の内容が非常に扱いづらいので、こちらの形式を扱いやすく変えた方が現実的です。
|
36
36
|
|
37
37
|
|
38
38
|
|
2
追記
test
CHANGED
@@ -33,3 +33,47 @@
|
|
33
33
|
|
34
34
|
|
35
35
|
`file.py`の内容が非常に扱いづらいので、こちらの形式を扱いやすく変えた方が現実的です。
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
例えば
|
40
|
+
|
41
|
+
---
|
42
|
+
|
43
|
+
json形式だと非常に楽です。[Wandbox](https://wandbox.org/permlink/CjDPo1bKOCHUUVTW)
|
44
|
+
|
45
|
+
```Python
|
46
|
+
|
47
|
+
import json
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
with open('file.json') as f:
|
52
|
+
|
53
|
+
data_dict = json.load(f)
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
print(data_dict)
|
58
|
+
|
59
|
+
```
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
**file.json**
|
64
|
+
|
65
|
+
```plain
|
66
|
+
|
67
|
+
{
|
68
|
+
|
69
|
+
"username1": "password1",
|
70
|
+
|
71
|
+
"username2": "password2"
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
```
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
もしファイルの生成・読み取りの自動化を狙っているのなら、こちらの方が良いでしょう。
|
1
修正
test
CHANGED
@@ -12,7 +12,7 @@
|
|
12
12
|
|
13
13
|
data_dict = {}
|
14
14
|
|
15
|
-
with open('file.
|
15
|
+
with open('file.txt') as f:
|
16
16
|
|
17
17
|
for row in f:
|
18
18
|
|