Python: Extract time from a file path -


i find general way extract time in format %h-%m-%s different file path follow:

u'c:/users/pivo/work/data/pink_exp_2015_12031329\\2015-12-03_15-58-29_trdata.txt'   u'c:/users/pivo/work/data/black_try\\2015-12-03_14-41-00_trdata.txt' 

i can use split , point time, 2 cases works follow looking general solution

date_str = path1.split('_')[2] date_str = path2.split('_')[4] 

what's best way that? thank you

import re  reg = r'\d+\-\d+\-\d+' data='c:/users/pivo/work/data/black_try\\2015-12-03_14-41-00_trdata.txt' a=re.findall(reg, data) print(a[1]) 

you need try doing regex patterns find absolutely want.

here python regex tutorial: http://www.tutorialspoint.com/python/python_reg_expressions.htm


Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -