从文件路径中提取 "time"
extracting the "time" from the file path
编辑上一个关于数据框的问题:
我有一个 pandas 数据框,其中一列包含文件路径,例如 path1/path2/path3/name/path4/hhmmss.tar.gz
。我想遍历每一行并提取文件路径中给定的名称和相应的小时、分钟和秒。
我的输出应该是这样的:
for i in dataframe.file_path:
code for extracting
print(name, hours, min, sec)
谢谢!
path = "archives/folder_name/name2/name3/name4/225422.tar.gz"
time = (path.split("/")[-1]).split(".")[0]
hours = time[0:2]
mins = time[2:4]
seconds = time[4:6]
例如:
import re
path = 'archives/folder_name/name2/name3/name4/225422.tar.gz'
pattern = re.compile(r'/(?P<hours>\d\d)(?P<minutes>\d\d)(?P<seconds>\d\d)\.tar\.gz$')
match = re.search(pattern, path)
print(match.groupdict() if match else None)
# {'hours': '22', 'minutes': '54', 'seconds': '22'}
fname = 'archives/folder_name/name2/name3/name4/225422.tar.gz'
pos = fname.rindex('/') + 1 # get the position of the character following the last /
h, m, s = [fname[i:i + 2] for i in range(pos, pos + 5, 2)] # generate a list of three slices of 2 characters each, using list compehention, starting from the position found earlier and assign it to three variables
print(h, m, s) # 22 54 22
编辑上一个关于数据框的问题:
我有一个 pandas 数据框,其中一列包含文件路径,例如 path1/path2/path3/name/path4/hhmmss.tar.gz
。我想遍历每一行并提取文件路径中给定的名称和相应的小时、分钟和秒。
我的输出应该是这样的:
for i in dataframe.file_path:
code for extracting
print(name, hours, min, sec)
谢谢!
path = "archives/folder_name/name2/name3/name4/225422.tar.gz"
time = (path.split("/")[-1]).split(".")[0]
hours = time[0:2]
mins = time[2:4]
seconds = time[4:6]
例如:
import re
path = 'archives/folder_name/name2/name3/name4/225422.tar.gz'
pattern = re.compile(r'/(?P<hours>\d\d)(?P<minutes>\d\d)(?P<seconds>\d\d)\.tar\.gz$')
match = re.search(pattern, path)
print(match.groupdict() if match else None)
# {'hours': '22', 'minutes': '54', 'seconds': '22'}
fname = 'archives/folder_name/name2/name3/name4/225422.tar.gz'
pos = fname.rindex('/') + 1 # get the position of the character following the last /
h, m, s = [fname[i:i + 2] for i in range(pos, pos + 5, 2)] # generate a list of three slices of 2 characters each, using list compehention, starting from the position found earlier and assign it to three variables
print(h, m, s) # 22 54 22