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

回答編集履歴

3

訂正

2018/08/21 08:34

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -5,7 +5,8 @@
5
5
  'HH:MM:SS'
6
6
  ```
7
7
 
8
- ご提示の正規表現は、『 \ あるいは . あるいは * 』を表現してしまっています。
8
+ ご提示の正規表現は、~~『 \ あるいは . あるいは * 』を表現してしまっています。~~
9
+ **訂正: **『 . あるいは * 』を表現してしまっています。
9
10
 
10
11
  もしくは
11
12
  ---

2

追記

2018/08/21 08:34

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -5,4 +5,26 @@
5
5
  'HH:MM:SS'
6
6
  ```
7
7
 
8
- ご提示の正規表現は、『 \ あるいは . あるいは * 』を表現してしまっています。
8
+ ご提示の正規表現は、『 \ あるいは . あるいは * 』を表現してしまっています。
9
+
10
+ もしくは
11
+ ---
12
+ [文字列のメソッド](https://docs.python.jp/3/library/stdtypes.html#string-methods)をうまく利用する方法が一つ。
13
+ ```Python
14
+ >>> ''.join(time.split('.')[:-1])
15
+ '17:26:30'
16
+ ```
17
+
18
+ [datetime](https://docs.python.jp/3/library/datetime.html)モジュールを利用しても良いでしょう。
19
+ ```Python
20
+ >>> import datetime as dt
21
+ >>>
22
+ >>> t = dt.datetime.strptime(time, '%H:%M:%S.%f')
23
+ >>> print(t)
24
+ 1900-01-01 17:26:30.123456
25
+ >>>
26
+ >>> t.strftime('%H:%M:%S')
27
+ '17:26:30'
28
+ ```
29
+
30
+ 参考: [Python 標準ライブラリ » datetime » strftime() と strptime() の振る舞い](https://docs.python.jp/3/library/datetime.html#strftime-strptime-behavior)

1

追記

2018/08/21 08:32

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -3,4 +3,6 @@
3
3
  >>> time = 'HH:MM:SS.SSSSSS'
4
4
  >>> re.sub(r'..+$', '', time)
5
5
  'HH:MM:SS'
6
- ```
6
+ ```
7
+
8
+ ご提示の正規表現は、『 \ あるいは . あるいは * 』を表現してしまっています。