回答編集履歴

2

c

2019/03/06 11:03

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -140,28 +140,16 @@
140
140
 
141
141
  [re --- 正規表現操作 — Python 3.7.2 ドキュメント](https://docs.python.org/ja/3/library/re.html)
142
142
 
143
-
144
-
145
143
  2. map() の使い方
146
144
 
147
-
148
-
149
- [Python の mapと filter ってなに? | Mastering Python](https://python.ms/type/for/map-filter/#_1-map)
145
+ [Python の mapと filter ってなに? | Mastering Python](https://python.ms/type/for/map-filter/#_1-map)
150
-
151
-
152
146
 
153
147
  3. `*` でタプル展開して、タプルの各要素を関数の引数としてわたす。
154
148
 
155
-
156
-
157
149
  [Pythonで関数の引数にリスト、タプル、辞書を展開して渡す | note.nkmk.me](https://note.nkmk.me/python-argument-expand/)
158
150
 
159
-
160
-
161
151
  4. datetime.datetime オブジェクトのコンストラクタ引数
162
152
 
163
-
164
-
165
153
  [datetime --- 基本的な日付型および時間型 — Python 3.7.2 ドキュメント](https://docs.python.org/ja/3/library/datetime.html#datetime.datetime)
166
154
 
167
155
 

1

d

2019/03/06 11:03

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -121,3 +121,105 @@
121
121
 
122
122
 
123
123
  ```
124
+
125
+
126
+
127
+ ## 追記
128
+
129
+
130
+
131
+ > 「dt = datetime(*map(int, match.groups()))」の部分は同いう意味なのでしょうか。
132
+
133
+
134
+
135
+ 理解するのに必要な項目
136
+
137
+
138
+
139
+ 1. 正規表現のグループ化と re.search()、その返り値の Match オブジェクトの使い方
140
+
141
+ [re --- 正規表現操作 — Python 3.7.2 ドキュメント](https://docs.python.org/ja/3/library/re.html)
142
+
143
+
144
+
145
+ 2. map() の使い方
146
+
147
+
148
+
149
+ [Python の mapと filter ってなに? | Mastering Python](https://python.ms/type/for/map-filter/#_1-map)
150
+
151
+
152
+
153
+ 3. `*` でタプル展開して、タプルの各要素を関数の引数としてわたす。
154
+
155
+
156
+
157
+ [Pythonで関数の引数にリスト、タプル、辞書を展開して渡す | note.nkmk.me](https://note.nkmk.me/python-argument-expand/)
158
+
159
+
160
+
161
+ 4. datetime.datetime オブジェクトのコンストラクタ引数
162
+
163
+
164
+
165
+ [datetime --- 基本的な日付型および時間型 — Python 3.7.2 ドキュメント](https://docs.python.org/ja/3/library/datetime.html#datetime.datetime)
166
+
167
+
168
+
169
+ ```
170
+
171
+ class datetime.datetime(year, month, day, hour, minute, second)
172
+
173
+ ```
174
+
175
+
176
+
177
+ その部分をわかりやすくしたコード
178
+
179
+
180
+
181
+ ```python
182
+
183
+ import re
184
+
185
+ from datetime import datetime
186
+
187
+
188
+
189
+ filename = 'aaa_1_1_20190302_110504.csv'
190
+
191
+ match = re.search('(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2}).csv', filename)
192
+
193
+
194
+
195
+ # 1. groups() で正規表現においてグループ化した部分の値をタプルで取得できる。
196
+
197
+ print(match.groups()) # ('2019', '03', '02', '11', '05', '04')
198
+
199
+
200
+
201
+ # 2. タプルの各値が str なので、map() で int に変換する。
202
+
203
+ args = tuple(map(int, match.groups()))
204
+
205
+ print(args) # (2019, 3, 2, 11, 5, 4)
206
+
207
+
208
+
209
+ # datetime.datetime クラスのコンストラクタに以下のように引数をわたしてもよいが、、、
210
+
211
+ year, month, day, hour, minutes, second = args
212
+
213
+ dt = datetime(year, month, day, hour, minutes, second)
214
+
215
+ print(dt) # 2019-03-02 11:05:04
216
+
217
+
218
+
219
+ # 3,4. タプル展開を利用するときれいにかける。
220
+
221
+ # datetime.datetime オブジェクトの位置引数の順序が year, month, day, hour, minute, second なので、タプルの0番目の要素は year、タプルの1番目の要素は month、... と渡してくれる。
222
+
223
+ dt = datetime(*args)
224
+
225
+ ```