On 2016-02-09 19:26, Anthony Papillion wrote: > myfile-2015-02-09-19-08-45-4223 > > Notice I'm replacing all of the "."'s, " "'s, and ":"'s returned by > datetime.now() with "-"'s. I'm doing that using the following code > but it's freaking ugly and I KNOW there is a better way to do it. I > just can't seem to think of it right now. Can anyone help? What is > the "right", or at least, less ugly, way to do this task? > > unprocessed_tag = str(datetime.datetime.now()) > removed_spaces = unprocessed_tag.split(" ") > intermediate_string = removed_spaces[0] + "-" + > removed_spaces[1] removed_colons = intermediate_string.split(":") > intermediate_string = removed_colons[0] + "-" + > removed_colons[1] > + "-" + removed_colons[2] > removed_dots = intermediate_string.split(".") > final_string = removed.dots[0] + "-" + removed_dots[1]
Why not format it the way you want to begin with? >>> datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S-%f") '2016-02-09-19-38-17-972532' -tkc -- https://mail.python.org/mailman/listinfo/python-list