Click here to Skip to main content
15,884,750 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Following is a date validation program. I'm trying to print the date in words.
I got the 'str' object is not callable' error at this line -->

av=date(day, month, year).strftime('%d %B %Y')

But I did convert day, month, & year into integers before using them in 'av'.

Proof:
print(type(day))
print(type(month))
print(type(year))

Output:
<class 'int'="">
<class 'int'="">
<class 'int'="">

Then why am I getting a 'string error'?

What I have tried:

from dateutil.parser import parse
import re
from datetime import datetime
from datetime import date

def date_fun(date):
  
  try:
    dt = parse(date)
    dt = dt.strftime('%d/%m/%Y')
    dmy_split=re.split('[- / ]', dt)
   
    day=int(dmy_split[0])
    month=int(dmy_split[1])
    year=int(dmy_split[2])

    av=date(day, month, year).strftime('%d %B %Y')

    if (day>=1 and day<=12 and month>=1 and month<=12 or len(year)==2):
                                                 print("Ambiguous; likely dates{}")                
    else:
                            print ("True,{}".format(av))                              

  except ValueError:        
                                                        
                                                     print("False")                                               

date_fun("abc")
date_fun("12/1/91")
date_fun("2002-9-18")
Posted
Updated 28-Aug-22 21:27pm
v2

Look at the error message:
'str' object is not callable' error at this line -->

av=date(day, month, year).strftime('%d %B %Y')
You are calling two functions here: date and strftime so one of those is wrong.
Break the line in two and see if that helps:
Python
xav=date(day, month, year)
av= xav.strftime('%d %B %Y')
Now you get the error on the first one:
Python
xav=date(day, month, year)
So you at the surrounding code:
Python
def date_fun(date):
  
  try:
    dt = parse(date)
...
    xav=date(day, month, year)
date is a parameter to the date_fun function, which means it overrides the function of the same name, so when you try to build a new date, it thinks you mean to use the variable.
Change the parameter name, and the error will go away:
Python
def date_fun(datestr):
  
  try:
    dt = parse(datestr)
    dt = dt.strftime('%d/%m/%Y')
    dmy_split=re.split('[- / ]', dt)
   
    day=int(dmy_split[0])
    month=int(dmy_split[1])
    year=int(dmy_split[2])
    av=date(day, month, year).strftime('%d %B %Y')

    if (day>=1 and day<=12 and month>=1 and month<=12 or len(year)==2):
                                                 print("Ambiguous; likely dates{}")                
    else:
                            print ("True,{}".format(av))                              

  except ValueError:        
                                                        
                                                     print("False")
And please, sort out your indentation!

You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!
 
Share this answer
 
Comments
CPallini 29-Aug-22 1:50am    
5.
I gave you the answer to this two days ago at Python date validation program - output query[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900