Home > Software engineering >  Python, format() function
Python, format() function

Time:08-12

Months={"01":"January","02":"February","03":"March","04":"April","05":"May","06":"June","07":"July","08":"August","09":"September","10":"October","11":"November","12":"December"}

date_time = lambda D: "{day} {month} {year} year {hour} hour" "{p1} " "{minute} minute" "{p2}".format(day=str(int(D.split('.')[0])),month=Months[D.split('.')[1]],year=D.split('.')[2].split(' ')[0],hour=str(int(D.split(' ')[1].split(':')[0])),p1=''if D.split(' ')[1].split(':')[0]=='01' else 's',minute=str(int(D.split(' ')[1].split(':')[1])),p2=''if D.split(' ')[1].split(':')[1]=='01' else 's')

how it should work :

date_time("01.01.2000 00:00") == "1 January 2000 year 0 hours 0 minutes"

how it does work:

date_time("01.01.2000 00:00") == "{day} {month} {year} year {hour} hour{p1} {minute} minute{p2}"

CodePudding user response:

If you must do it yourself, try f-strings?

MONTHS={1:"January", 2:"February", 3:"March", 4:"April", 5:"May", 6:"June", 7:"July", 8:"August", 9:"September", 10:"October", 11:"November", 12:"December"}

def format(day, month, year, hour, minute):
    return f"{day} {MONTHS[month]} {year} {hour} hour{('s' if hour > 1 else '')} {minute} minute{('s' if minute > 1 else '')}"

Otherwise, Python has a builtin package called datetime which may be of use...

CodePudding user response:

You're only calling format() on the last string "{p2"} because . has higher precedence than . You need to put the concatenations in parentheses.

date_time = lambda D: ("{day} {month} {year} year {hour} hour" "{p1} " "{minute} minute" "{p2}").format(day=str(int(D.split('.')[0])),month=Months[D.split('.')[1]],year=D.split('.')[2].split(' ')[0],hour=str(int(D.split(' ')[1].split(':')[0])),p1=''if D.split(' ')[1].split(':')[0]=='01' else 's',minute=str(int(D.split(' ')[1].split(':')[1])),p2=''if D.split(' ')[1].split(':')[1]=='01' else 's')

Although I don't understand why you're concatenating a bunch of literal strings. Just make it one long string.

date_time = lambda D: "{day} {month} {year} year {hour} hour{p1} {minute} minute{p2}".format(day=str(int(D.split('.')[0])),month=Months[D.split('.')[1]],year=D.split('.')[2].split(' ')[0],hour=str(int(D.split(' ')[1].split(':')[0])),p1=''if D.split(' ')[1].split(':')[0]=='01' else 's',minute=str(int(D.split(' ')[1].split(':')[1])),p2=''if D.split(' ')[1].split(':')[1]=='01' else 's')

CodePudding user response:

It is better if you use Template from string.

# import Template
from string import Template


date = "01.01.2000 00:00"
day = date.split('.')[0]
month = date.split('.')[1]
year = date.split('.')[2].split(' ')[0]
p1 = date.split(' ')[1].split(':')[0]
p2 = date.split(':')[1]

Months={
    "01":"January",
    "02":"February",
    "03":"March",
    "04":"April",
    "05":"May",
    "06":"June",
    "07":"July",
    "08":"August",
    "09":"September",
    "10":"October",
    "11":"November",
    "12":"December"
}

# Creating Template
template = Template("$day $month $year year $hour hours $minute minutes")

# Using Template
date = template.substitute({
    'day' : int(day),
    'month' : Months[month],
    'year' : year,
    'hour' : int(p1),
    'minute' : int(p2),
})

print(date) # 1 January 2000 year 0 hours 0 minutes

CodePudding user response:

The datetime module exists for this purpose, use it!

from datetime import datetime

dt = datetime.strptime("01.01.2000 00:00", "%m.%d.%Y %H:%M")
print(dt.strftime("%-m %B %Y year %-H hours %-M minutes"))

Output:

1 January 2000 year 0 hours 0 minutes

CodePudding user response:

This usage is slightly wrong. It must go like this

date_time = lambda D: "{} {} {} year {} hour" "{} " "{} minute" "{}".format(day=str(int(D.split('.')[0])),month=Months[D.split('.')[1]],year=D.split('.')[2].split(' ')[0],hour=str(int(D.split(' ')[1].split(':')[0])),p1=''if D.split(' ')[1].split(':')[0]=='01' else 's',minute=str(int(D.split(' ')[1].split(':')[1])),p2=''if D.split(' ')[1].split(':')[1]=='01' else 's')

You don't have to write the variable names between bracelets when you are using format() function.

  • Related