Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Tim Chase Newsgroups: comp.lang.python Subject: Re: There has to be a better way to split this string! Date: Tue, 9 Feb 2016 19:39:47 -0600 Lines: 28 Message-ID: References: <56BA91B5.5090400@cajuntechie.org> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de AWf5efSqnOsBFkCTkaGbWgpcsrwzdD1w9khTWWG72y3g== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'ugly': 0.07; 'cc:addr :python-list': 0.09; 'subject:string': 0.09; '"-"': 0.16; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'help?': 0.16; 'received:10.21': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sfxlen:0': 0.16; 'subject:There': 0.16; 'with?': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'cc:no real name:2**0': 0.22; 'replacing': 0.23; 'header:In-Reply-To:1': 0.24; 'skip:m 30': 0.27; 'skip:u 20': 0.28; "i'm": 0.30; 'code': 0.30; 'anyone': 0.32; "can't": 0.32; 'returned': 0.32; "skip:' 20": 0.34; 'but': 0.36; 'there': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'charset:us-ascii': 0.37; 'seem': 0.37; 'doing': 0.38; 'why': 0.39; 'format': 0.39; 'skip:u 10': 0.61; 'received:46': 0.63; 'ugly,': 0.84; 'subject:this': 0.85 X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-Sender-Id: wwwh|x-authuser|tim@thechases.com X-MC-Relay: Neutral X-MailChannels-SenderId: wwwh|x-authuser|tim@thechases.com X-MailChannels-Auth-Id: wwwh X-MC-Loop-Signature: 1455068547109:1995909187 X-MC-Ingress-Time: 1455068547109 In-Reply-To: <56BA91B5.5090400@cajuntechie.org> X-Mailer: Claws Mail 3.11.1 (GTK+ 2.24.25; x86_64-pc-linux-gnu) X-AuthUser: tim@thechases.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102750 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