I want to split on Python several times

Asked 1 years ago, Updated 1 years ago, 131 views

0       2003-12-31T15:34:07.200Z
1       2003-12-23T00:20:34.800Z
2       2003-12-20T12:13:36.300Z
3       2003-12-17T04:19:16.400Z
4       2003-12-16T09:07:56.100Z
5       2003-12-15T18:55:05.100Z

We're going to do "2013", "12", "31", and "15:334:07" "Year", "Mon", "Sun", and "Time" But the split that I know I can only cut it in one place, so I ask for your help.

split python

2022-09-22 18:19

2 Answers

If you can't do it once, you can do it twice.

s = '2003-12-31T15:34:07.200Z'
yyyymmdd, t24 = s.split('T')
yyyy, mm, dd = yyyymmdd.split('-')
print(yyyy, mm, dd)
print(t24)

2003 12 31
15:34:07.200Z


2022-09-22 18:19

You can also use regular expressions.

import re

date = "2003-12-31T15:34:07.200Z"
result = re.split(r"-|T|\.", date)

print(result)

You can cut by entering the separator you want to cut as shown above.

where r"-|T|\." means to truncate based on - or T or . (| means or ).


2022-09-22 18:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.