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


Groups > comp.lang.python > #102746

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

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Newsgroups comp.lang.python
Subject Re: There has to be a better way to split this string!
Date Wed, 10 Feb 2016 01:45:42 +0000
Lines 36
Message-ID <mailman.10.1455068769.7749.python-list@python.org> (permalink)
References <56BA91B5.5090400@cajuntechie.org>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de IUyXoq4wuJohkwqX5ROUqQ5VzeIHhkOtAE1QbmyaWjBQ==
Return-Path <oscar.j.benjamin@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'ugly': 0.07; 'cc:addr :python-list': 0.09; 'filename.': 0.09; 'strftime': 0.09; 'subject:string': 0.09; '"-"': 0.16; '2016': 0.16; 'cc:name:python list': 0.16; "chris'": 0.16; 'help?': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sfxlen:0': 0.16; 'subject:There': 0.16; 'wrote:': 0.16; 'string': 0.17; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'assuming': 0.22; 'replacing': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'skip:m 30': 0.27; 'message-id:@mail.gmail.com': 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; 'file': 0.34; 'received:google.com': 0.35; 'something': 0.35; 'but': 0.36; 'needed': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'suggestion': 0.37; 'seem': 0.37; 'doing': 0.38; 'version': 0.38; 'received:209': 0.38; 'skip:s 40': 0.38; 'skip:u 10': 0.61; 'default': 0.61; 'here': 0.66; 'oscar': 0.84; 'ugly,': 0.84; 'subject:this': 0.85
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=zYSqpvGWZEHZFbeqB4qsTpKN5Cq0AvWyH48EIytTgDI=; b=hrqxzuQhRLg0C0ja0EzVJ8WcOEKFTelZnsyFgIoVZYLnfBfCR4zYednARqX8JnY2vw HPN0rVzLHBi+dpYce0Xnzes/l33CMpOJvjW3GnFijh/VNuT+/0ms3sH+IWOZkY9xXv0s TXzlEgNEYTnvsjysEKClVrAOcG2Lao+GH+ltC+DqB5W+fZIVMCeDFHAUcsnf4qRv3IoN BflZkwLOh0sbvfV9d4dOA/QKoybeWoDK3d1vwkGZlj2h7YBM4Tc4RnOeeyCKWOkzRmys YOHOQpYQldzVkgbcnQeYb8xBbJwGHQlBo0RfYwNtMqyR6ry60weEa0t4DqmEAbNZf5QQ 8bcg==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=zYSqpvGWZEHZFbeqB4qsTpKN5Cq0AvWyH48EIytTgDI=; b=TCGBPMKQilAOBw4wgfQ84AfptG+b7TJvthBiyIMwhcJIVLU7r4HnWlNp6osIpsF9tZ 6NwaX1wedjpKS6BR+xqI3Il98UKAILJlcaqE7apMs2Q3oh6vzjJkSLXMuq82SKYyadVV SHo3K3Eyx1njCsRoq36mcfFt9j9x6DPNohNGYjSlMl/n4X6CLNh7gWMn2fN5Omr8EoV4 4O/VLngOkq4tTpt8jgqbMBPOsFaf8bDi05t9nB6jX3RbKUV2xW9o+dEaIO59yCbUjSg1 96+YCcG9Ml2yg4sm50GXtLlvJNYdUnx0lsEN0ZB57kZeQgJ+eVIQWiSVZWdUUOZF7d5T n1Kw==
X-Gm-Message-State AG10YOQK7cbZHPSKq2f+RL6nAFdicIUCcQ3q3/SJc906e7boVSwiECS+CWRvxScYdXgSYCiMkUCq8a0JVJ2yfQ==
X-Received by 10.112.166.100 with SMTP id zf4mr14994029lbb.58.1455068762278; Tue, 09 Feb 2016 17:46:02 -0800 (PST)
In-Reply-To <56BA91B5.5090400@cajuntechie.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21rc2
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:102746

Show key headers only | View raw


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 comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web