回答編集履歴
2
c
answer
CHANGED
@@ -69,17 +69,11 @@
|
|
69
69
|
|
70
70
|
1. 正規表現のグループ化と re.search()、その返り値の Match オブジェクトの使い方
|
71
71
|
[re --- 正規表現操作 — Python 3.7.2 ドキュメント](https://docs.python.org/ja/3/library/re.html)
|
72
|
-
|
73
72
|
2. map() の使い方
|
74
|
-
|
75
|
-
[Python の mapと filter ってなに? | Mastering Python](https://python.ms/type/for/map-filter/#_1-map)
|
73
|
+
[Python の mapと filter ってなに? | Mastering Python](https://python.ms/type/for/map-filter/#_1-map)
|
76
|
-
|
77
74
|
3. `*` でタプル展開して、タプルの各要素を関数の引数としてわたす。
|
78
|
-
|
79
75
|
[Pythonで関数の引数にリスト、タプル、辞書を展開して渡す | note.nkmk.me](https://note.nkmk.me/python-argument-expand/)
|
80
|
-
|
81
76
|
4. datetime.datetime オブジェクトのコンストラクタ引数
|
82
|
-
|
83
77
|
[datetime --- 基本的な日付型および時間型 — Python 3.7.2 ドキュメント](https://docs.python.org/ja/3/library/datetime.html#datetime.datetime)
|
84
78
|
|
85
79
|
```
|
1
d
answer
CHANGED
@@ -59,4 +59,55 @@
|
|
59
59
|
[datetime.datetime(2019, 3, 2, 11, 10), ['test\acb_2_4_20190302_111003.csv']],
|
60
60
|
[datetime.datetime(2019, 3, 3, 11, 10), ['test\cbz_2_6_20190303_111004.csv']]]
|
61
61
|
|
62
|
+
```
|
63
|
+
|
64
|
+
## 追記
|
65
|
+
|
66
|
+
> 「dt = datetime(*map(int, match.groups()))」の部分は同いう意味なのでしょうか。
|
67
|
+
|
68
|
+
理解するのに必要な項目
|
69
|
+
|
70
|
+
1. 正規表現のグループ化と re.search()、その返り値の Match オブジェクトの使い方
|
71
|
+
[re --- 正規表現操作 — Python 3.7.2 ドキュメント](https://docs.python.org/ja/3/library/re.html)
|
72
|
+
|
73
|
+
2. map() の使い方
|
74
|
+
|
75
|
+
[Python の mapと filter ってなに? | Mastering Python](https://python.ms/type/for/map-filter/#_1-map)
|
76
|
+
|
77
|
+
3. `*` でタプル展開して、タプルの各要素を関数の引数としてわたす。
|
78
|
+
|
79
|
+
[Pythonで関数の引数にリスト、タプル、辞書を展開して渡す | note.nkmk.me](https://note.nkmk.me/python-argument-expand/)
|
80
|
+
|
81
|
+
4. datetime.datetime オブジェクトのコンストラクタ引数
|
82
|
+
|
83
|
+
[datetime --- 基本的な日付型および時間型 — Python 3.7.2 ドキュメント](https://docs.python.org/ja/3/library/datetime.html#datetime.datetime)
|
84
|
+
|
85
|
+
```
|
86
|
+
class datetime.datetime(year, month, day, hour, minute, second)
|
87
|
+
```
|
88
|
+
|
89
|
+
その部分をわかりやすくしたコード
|
90
|
+
|
91
|
+
```python
|
92
|
+
import re
|
93
|
+
from datetime import datetime
|
94
|
+
|
95
|
+
filename = 'aaa_1_1_20190302_110504.csv'
|
96
|
+
match = re.search('(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2}).csv', filename)
|
97
|
+
|
98
|
+
# 1. groups() で正規表現においてグループ化した部分の値をタプルで取得できる。
|
99
|
+
print(match.groups()) # ('2019', '03', '02', '11', '05', '04')
|
100
|
+
|
101
|
+
# 2. タプルの各値が str なので、map() で int に変換する。
|
102
|
+
args = tuple(map(int, match.groups()))
|
103
|
+
print(args) # (2019, 3, 2, 11, 5, 4)
|
104
|
+
|
105
|
+
# datetime.datetime クラスのコンストラクタに以下のように引数をわたしてもよいが、、、
|
106
|
+
year, month, day, hour, minutes, second = args
|
107
|
+
dt = datetime(year, month, day, hour, minutes, second)
|
108
|
+
print(dt) # 2019-03-02 11:05:04
|
109
|
+
|
110
|
+
# 3,4. タプル展開を利用するときれいにかける。
|
111
|
+
# datetime.datetime オブジェクトの位置引数の順序が year, month, day, hour, minute, second なので、タプルの0番目の要素は year、タプルの1番目の要素は month、... と渡してくれる。
|
112
|
+
dt = datetime(*args)
|
62
113
|
```
|