[英]How to extract date from the filename using Python?
I am trying to extract the date from this filename format:我正在尝试从此文件名格式中提取日期:
"output_AU1001-BY.20160502.csv"
I thought maybe I can do it with re.findall()
but could not do it.我想也许我可以用
re.findall()
做到这一点,但不能做到。
Example without regex没有正则表达式的例子
Code :代码:
s = 'output_AU1001-BY.20160502.csv'
date = s.split(".")[1]
print date
Output :输出:
>>> 20160502
Here is one way:这是一种方法:
import re
d = 'output_AU1001-BY.20160502.csv'
d = re.sub('output_[A-Z]+[0-9]+-BY[.]([0-9]+)[.]csv$', r'\1', d)
print d
Assumptions:假设:
output_
is constant for all file names output_
对于所有文件名都是常量AU
is variable, but will always be ALL CAPS AU
是可变的,但总是全部大写-BY.
is constant for all file names20160502
is variable, but will always be all digits 20160502
是可变的,但始终都是数字.csv
is constant for all file names .csv
是所有文件名的常量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.