Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: There has to be a better way to split this string! Date: Wed, 10 Feb 2016 12:34:54 +1100 Lines: 30 Message-ID: References: <56BA91B5.5090400@cajuntechie.org> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 5df2P79Sca/13jV4ZM7LEweCWeDK84ASbWOs20c0qjNQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'ugly': 0.07; 'cc:addr :python-list': 0.09; 'filename.': 0.09; 'okay': 0.09; 'subject:string': 0.09; 'wed,': 0.15; '2016': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'help?': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:There': 0.16; 'sure.': 0.16; 'wrote:': 0.16; 'string': 0.17; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'feb': 0.23; 'replacing': 0.23; 'header:In-Reply-To:1': 0.24; 'skip:m 30': 0.27; 'message-id:@mail.gmail.com': 0.27; "i'm": 0.30; 'code': 0.30; 'anyone': 0.32; "can't": 0.32; 'returned': 0.32; 'url:python': 0.33; "skip:' 20": 0.34; 'file': 0.34; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'options:': 0.35; 'something': 0.35; 'but': 0.36; 'instead': 0.36; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'url:library': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'seem': 0.37; 'doing': 0.38; "won't": 0.38; 'version': 0.38; 'received:209': 0.38; 'format': 0.39; 'some': 0.40; 'url:3': 0.60; 'your': 0.60; 'chrisa': 0.84; 'off,': 0.84; 'ugly,': 0.84; 'subject:this': 0.85; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=cMwQ69sDlWgs67BnbpcqcgqfYqHm5SFN0K2veKheEh4=; b=yPUwCBXu0i82sXqlUcGceL6Z7q8bweQ8MN6W5UcdjYFuDvbWO1YNcqF6fuqgiqL0VC h80LV5qYRkwHTyrDnVrufFFMWg1umvY0GQnTMh5MYfto1gpEk/e1RGcgaUYVsErV3BAP DtcsonsLj5XGQcm10MC37lCsri+2lD5UJOHt5heAXBtgmH/90Q56KWitreuD2zRdUijR /UecXZVHGC3gu1Hv4OvXcfc5esA1SnOiIYJDUyt0xoZGxTTn4iN/A/BJgRyx/EWMd1g3 e0vLMmfL4fQpxR7s2RuR3m49XlWFZ09zwkl2rAa7x3Y6n0jfLUeNvHgYJIwHFx7wGIll EBpg== 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:date :message-id:subject:from:cc:content-type; bh=cMwQ69sDlWgs67BnbpcqcgqfYqHm5SFN0K2veKheEh4=; b=dEyF3eY2tfCjX+2PlGxPWCIMYBU6GBxUaezUSlbZkSZPyMFjCwozeTlSEqzisa1vqK hV1Dm2eH/DYNPN68fAYaKhZ4ee76sjXz8IiekKFfpuldKFg/0+7+xHkZAfQywu96Hjqk fqwrNvyNxl+25XGmxJGtfJJApdqc6dnHVeu7ygb9E5WA00JCU4gg9tDfMYPmW+Y7zawD fHuqtITc640Z1tmLkb8OCYRNVwuW7zKta/u6ptAm5r1a+F9wP9Z8MevMeZnZHcoE31lR 8rZybwBwTjdyiAD33aLIhLn0MJuJrCFSEKKaqmhEwJfAzzh6TPpL/dz6Zgm8FQFFisyv 11dg== X-Gm-Message-State: AG10YORjQfGqE9UJBxGevj0PzamxA3BhaIKnK/7GUZt+/XN6bDM5ZGgVsEKVZylYY3LY+/sA82b2sXgI6UD/DQ== X-Received: by 10.50.111.169 with SMTP id ij9mr7664569igb.92.1455068095033; Tue, 09 Feb 2016 17:34:55 -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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102744 On Wed, Feb 10, 2016 at 12:26 PM, Anthony Papillion wrote: > I am using datetime.now() to create a unique version of a filename. First off, be aware that this won't make a unique file name. But if you're okay with that (deal with collisions somehow), then sure. > 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? Instead of using str(), use strftime(): >>> now = datetime.datetime.now() >>> str(now) '2016-02-10 12:34:26.377701' >>> now.strftime("%Y-%m-%d-%H-%M-%S-%f") '2016-02-10-12-34-26-377701' You could instead use some other format string if you like. Here's your options: https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior ChrisA