回答編集履歴
5
追記
answer
CHANGED
@@ -38,4 +38,23 @@
|
|
38
38
|
}
|
39
39
|
```
|
40
40
|
|
41
|
-
もしファイルの生成・読み取りの自動化を狙っているのなら、こちらの方が良いでしょう。
|
41
|
+
もしファイルの生成・読み取りの自動化を狙っているのなら、こちらの方が良いでしょう。
|
42
|
+
|
43
|
+
今更だけど
|
44
|
+
---
|
45
|
+
ちょっとだけシンプルにリライトしました。[Wandbox](https://wandbox.org/permlink/71e9UqyPjBAxVy3Q)
|
46
|
+
```Python
|
47
|
+
import re
|
48
|
+
|
49
|
+
pattern = re.compile(r"""([\w|'|,]+)""")
|
50
|
+
|
51
|
+
data_dict = {}
|
52
|
+
with open('file.txt') as f:
|
53
|
+
for row in f:
|
54
|
+
find_obj = re.findall(pattern, row)[0]
|
55
|
+
|
56
|
+
key, value = map(lambda x: x.strip("(')"), find_obj.split(','))
|
57
|
+
data_dict[key] = value
|
58
|
+
|
59
|
+
print(data_dict)
|
60
|
+
```
|
4
自信ないので
answer
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
無理矢理書いてみました。 [Wandbox](https://wandbox.org/permlink/n3IcJA8Ud06jmsoP)
|
2
|
+
正規表現に苦手意識があるので、無駄な処理をしてしまっている気がしますが。
|
2
3
|
```Python
|
3
4
|
import re
|
4
5
|
|
3
修正
answer
CHANGED
@@ -15,7 +15,7 @@
|
|
15
15
|
print(data_dict)
|
16
16
|
```
|
17
17
|
|
18
|
-
`file.
|
18
|
+
`file.txt`の内容が非常に扱いづらいので、こちらの形式を扱いやすく変えた方が現実的です。
|
19
19
|
|
20
20
|
例えば
|
21
21
|
---
|
2
追記
answer
CHANGED
@@ -15,4 +15,26 @@
|
|
15
15
|
print(data_dict)
|
16
16
|
```
|
17
17
|
|
18
|
-
`file.py`の内容が非常に扱いづらいので、こちらの形式を扱いやすく変えた方が現実的です。
|
18
|
+
`file.py`の内容が非常に扱いづらいので、こちらの形式を扱いやすく変えた方が現実的です。
|
19
|
+
|
20
|
+
例えば
|
21
|
+
---
|
22
|
+
json形式だと非常に楽です。[Wandbox](https://wandbox.org/permlink/CjDPo1bKOCHUUVTW)
|
23
|
+
```Python
|
24
|
+
import json
|
25
|
+
|
26
|
+
with open('file.json') as f:
|
27
|
+
data_dict = json.load(f)
|
28
|
+
|
29
|
+
print(data_dict)
|
30
|
+
```
|
31
|
+
|
32
|
+
**file.json**
|
33
|
+
```plain
|
34
|
+
{
|
35
|
+
"username1": "password1",
|
36
|
+
"username2": "password2"
|
37
|
+
}
|
38
|
+
```
|
39
|
+
|
40
|
+
もしファイルの生成・読み取りの自動化を狙っているのなら、こちらの方が良いでしょう。
|
1
修正
answer
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
pattern = re.compile(r"""(?<=()'\w+'.'\w+'(?=))""")
|
6
6
|
|
7
7
|
data_dict = {}
|
8
|
-
with open('file.
|
8
|
+
with open('file.txt') as f:
|
9
9
|
for row in f:
|
10
10
|
find_obj = re.findall(pattern, row)[0]
|
11
11
|
|