2

I've come across several answers (1, 2, 3) regarding the removal of leading zeros on python3 datetime objects.

One of the most voted answers states:

On Windows, you would use #, e.g. %Y/%#m/%#d

The code above doesn't work for me. I've also tried the Linux solution, which uses - instead of #, without success.

Code:

loop_date = "1950-1-1"
date_obj = datetime.strptime(loop_date, '%Y-%m-%d') # or '%Y-%#m-%#d' which produces the errors below
date_obj += timedelta(days=1)
print(date_obj)
# This the prints `1954-01-02` but I need `1954-1-2`

Traceback:

Traceback (most recent call last):
  File "C:/collect_games.py", line 84, in <module>
    date_obj = datetime.strptime(loop_date, '%Y-%-m-%d')
  File "E:\Anaconda3\lib\_strptime.py", line 565, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "E:\Anaconda3\lib\_strptime.py", line 354, in _strptime
    (bad_directive, format)) from None
ValueError: '#' is a bad directive in format '%Y-%#m#%d'

What's most pythonic approach to this problem?

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • you are parsing a time datetime here ... the `-` is applied when formatting an datetime into a string .... python does not need `02` inside a string to understands its the month or whatever ... to parse the month from a string into a datetime object ? – Patrick Artner Apr 05 '18 at 14:32
  • remove the `-` between `%` and `m`. – Mazdak Apr 05 '18 at 14:33
  • I need the output to be formatted without the leading zero, which doesn't happen with my current code.. – Pedro Lobito Apr 05 '18 at 14:33
  • @Kasramvd in php you can use `j` for Day of the month without leading zeros or `d` for Day of the month with leading zeros – Pedro Lobito Apr 05 '18 at 14:34
  • You're creating a `datetime` object with a standard representation. Removing those zeros is another problem and related to the string representation of the object. – Mazdak Apr 05 '18 at 14:35
  • If you want to create a string representation from a datetime object without padding zeros you might want to use `strftime` method as is mentioned in @Patrick's answer. – Mazdak Apr 05 '18 at 14:41
  • The code on Patrick's answer, and the one on my question, runs fine on online compilers, but not on my windows python3.6 installation. I don't have a solution yet, but now I'm this is an installation problem rather than a syntax one. – Pedro Lobito Apr 05 '18 at 14:48
  • @PedroLobito - see edit – Patrick Artner Apr 05 '18 at 14:49

1 Answers1

7

You are confusing the formats of parsing (a string into a dt) and formatting (a dt into a string):

This works on linux (or online via http://pyfiddle.io):

import datetime 

dt = datetime.datetime.now()

# format datetime as string
print(datetime.datetime.strftime(dt, '%Y-%-m-%-d'))  # - acts to remove 0 AND as delimiter

# parse a string into a datetime object
dt2 = datetime.datetime.strptime("1022-4-09", '%Y-%m-%d')

print(dt2)

Output:

2018-4-5
1022-04-09 00:00:00

The - when formatting a string acts to remove the leading 0 AND as delimiter - for parsing it only needs to be placed as delimiter - parsing works on on either 02 or 2 for %m


This works on Windows (VS2017):

from datetime import datetime, timedelta

loop_date = "1950-1-1"
date_obj = datetime.strptime(loop_date, '%Y-%m-%d')
date_obj += timedelta(days=1)
print(date_obj)   # output the datetime-object

print(datetime.strftime(date_obj,'%Y-%#m-%#d'))  # output it formatted

Output:

1950-01-02 00:00:00
1950-1-2
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Your code produces : `Traceback (most recent call last): File "C:/collect_games.py", line 7, in print(datetime.datetime.strftime(dt, '%Y-%-m-%d')) ValueError: Invalid format string ` on `python 3.6` and there's nothing different from the question's code, which yields the same error. – Pedro Lobito Apr 05 '18 at 14:39
  • @PedroLobito copy my code into http://www.pyfiddle.io - it works as 2.7 and 3.6 ? – Patrick Artner Apr 05 '18 at 14:41
  • Your code runs fine but not on my windows python3.6 . I'm almost sure this is an installation problem rather than a syntax one. – Pedro Lobito Apr 05 '18 at 14:45
  • @PedroLobito See edit. pyfiddle.io is a linux server – Patrick Artner Apr 05 '18 at 14:47
  • ***This works on Windows (VS2017)*** it does. Strange, because I've tried that...I guess I've to apply the "removal filter" (`-`) only at `print` time. I'll mark your answer as correct. Thank you. – Pedro Lobito Apr 05 '18 at 14:52
  • @PedroLobito RemoveAll - Filter would be `#` ;) for windows. Sorry for the confusion with linus syntax. – Patrick Artner Apr 05 '18 at 14:54
  • NP, all set, appreciated! – Pedro Lobito Apr 05 '18 at 18:53