Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102746 > unrolled thread
| Started by | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
|---|---|
| First post | 2016-02-10 01:45 +0000 |
| Last post | 2016-02-10 01:45 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: There has to be a better way to split this string! Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2016-02-10 01:45 +0000
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
|---|---|
| Date | 2016-02-10 01:45 +0000 |
| Subject | Re: There has to be a better way to split this string! |
| Message-ID | <mailman.10.1455068769.7749.python-list@python.org> |
On 10 February 2016 at 01:26, Anthony Papillion <anthony@cajuntechie.org> wrote:
> 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
Chris' suggestion to use strftime is better but assuming you really
needed to work with the default string then there are easier ways
e.g.:
>>> from datetime import datetime
>>> str(datetime.now()).translate(str.maketrans(': .', '---'))
'2016-02-10-01-44-54-244789'
--
Oscar
Back to top | Article view | comp.lang.python
csiph-web