Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102750 > unrolled thread

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

Started byTim Chase <python.list@tim.thechases.com>
First post2016-02-09 19:39 -0600
Last post2016-02-09 19:39 -0600
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.


Contents

  Re: There has to be a better way to split this string! Tim Chase <python.list@tim.thechases.com> - 2016-02-09 19:39 -0600

#102750 — Re: There has to be a better way to split this string!

FromTim Chase <python.list@tim.thechases.com>
Date2016-02-09 19:39 -0600
SubjectRe: There has to be a better way to split this string!
Message-ID<mailman.14.1455069687.7749.python-list@python.org>
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


[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web