要从 ISOString 中提取时间子字符串,可以使用正则表达式来匹配时间部分并提取出来。以下是一个示例代码:
import re
def extract_time_from_isostring(isostring):
# 定义时间正则表达式
time_regex = r'T(\d{2}:\d{2}:\d{2})'
# 使用正则表达式匹配时间部分
match = re.search(time_regex, isostring)
if match:
# 提取匹配到的时间部分
time = match.group(1)
return time
else:
return None
# 示例使用
isostring = '2022-01-01T12:34:56.789Z'
time = extract_time_from_isostring(isostring)
print(time) # 输出:12:34:56
在上面的代码中,我们使用正则表达式 T(\d{2}:\d{2}:\d{2})
匹配 ISOString 中的时间部分。其中,T
表示字符 'T',\d{2}
表示两个数字,:
表示字符 ':'。所以,\d{2}:\d{2}:\d{2}
表示一个格式为 HH:MM:SS
的时间部分。
然后,我们使用 re.search()
函数在 ISOString 中匹配时间部分。如果匹配成功,则使用 match.group(1)
来提取匹配到的时间部分。最后,我们将提取到的时间打印出来。
请注意,这个方法是基于 ISOString 的时间部分格式为 HH:MM:SS
,如果 ISOString 的时间部分格式有所不同,可能需要调整正则表达式来适应不同的格式。
下一篇:按确定的元素将pandas行拆分