string - Modifying data in a column Python -
hi guys have column this,
start start = 11122001 start = 12012014 start = 23122001
and want remove "start =" , date format
start 11/12/2001 12/01/2014 23/12/2001
how do properly?
it depends on trying do.
if want remove start =
each line:
lines = [ format_date(re.sub("^start =", '', line)) line in lines ]
(presuming have text line line in list).
to format date need implement function format_date
convert dates 11122001
11/12/2001
.
there several ways how depending on input format. 1 of solutions:
def format_date(x): if re.match(x, '[0-9]{8}'): return "/".join([x[:2], x[2:4], x[4:]]) else: return x
you check first if line match date expression (looks date), , if does, rewrite it. otherwise return is.
of course, can combine solution in 1 line , don't use function @ all, in case not clear.
another, map
-based solution:
def format_line(x): x = re.sub("^start =", '', line) if re.match(x, '[0-9]{8}'): return "/".join([x[:2], x[2:4], x[4:]]) else: return x map(format_line, lines)
Comments
Post a Comment