【发布时间】:2021-01-05 05:08:03
【问题描述】:
我有不同的路径,我必须使用正则表达式从路径中找到日期并将其附加到文件名。
Input:
/hash/20190811T003013/ABC.txt
Expected Output:
ABC_20190811T003013.txt
【问题讨论】:
我有不同的路径,我必须使用正则表达式从路径中找到日期并将其附加到文件名。
Input:
/hash/20190811T003013/ABC.txt
Expected Output:
ABC_20190811T003013.txt
【问题讨论】:
re.findall的解决方案:
import re
s = '/hash/20190811T003013/ABC.txt'
re.findall('(\w+).txt', s)[0] + '_' + re.findall('(\d{8}\w+)', s)[0] + re.findall('(\.\w+)', s)[0]
'ABC_20190811T003013.txt'
【讨论】:
尝试将str.join 与map 和str.split 一起使用:
s = '/hash/20190811T003013/ABC.txt'
_, b, c, d = s.split('/')
print('_'.join(d.split('.')[0], c) + '.txt')
输出:
ABC_20190811T003013.txt
【讨论】: