There has to be a better way to split this string!

Anthony Papillion anthony at cajuntechie.org
Tue Feb 9 20:26:13 EST 2016


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

Hello Everyone,

I am using datetime.now() to create a unique version of a filename.
When the final file is named, it will look something like:

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?

Here is the code I'm using:


    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]

    return final_string

Thanks!
Anthony

-----BEGIN PGP SIGNATURE-----

iQIcBAEBCgAGBQJWupG0AAoJEAKK33RTsEsVVJEQAKZEk5jolpxa4plDvW6rY9ux
YdXHxtBa3emLprVMJTAg6+NJLZaeBjulttWv2Vy1y2tE8j1prLqHVtzITdX5kVG5
z0VUZgLRYi9Ocfk5vz4lGsWEWwRtkUQrpodbXqCArerUkBqHEW/Bgs2PGq9FGbNP
6QCWLsNr1APkEl5q8P3QCM7x1z0nsQKsbVMdvNjfi2kzsvDIm6lXgl9uUFxOFesp
fUkuiLFHFpbIRnnMke2mLahXWyN14QNmw6OOkqG963gOjecfC+2dNozAOSf42ul+
s2p9h3BiwijMvU/nvc/6jR3uFmoE3dIcGWsyYFNPWnPvrHUy0qHIgcavWDEo3+cw
J+qOYSX3XlTIrGc5ZJKdJWs6FH7d1+WZ4GndSj316LAKq0XN46rq/ppJksStpUwU
I7Gmk9mAnRt9uZy25xsx93jY8IiiOKDmI1cTclo/UKAYvQk8ib/allLnVZrssmsc
2hgugwBe6R966wavdrYl/yt1VlfwRdImRUL6pTNnCDMYYcJS0F7ATJ/dWV9iRD0h
PiIiB6zB61EyKw1djBRQ0F8XqxNZK0qR0UMR5hPWJcHRp3y94bgl2tI0/aMXmFW+
jiwQ4ecNgqb9k2C6iok+9OXJ3YoqToVbXFS7/svXpS88giksS+Re+5RChxZGSWSd
gXhYvKEHs+2l3Vuyhdsr
=8uK+
-----END PGP SIGNATURE-----




More information about the Python-list mailing list