Fromisoformat: argument must be str

The “fromisoformat” method is a built-in Python method that is used to convert a string into a datetime object. It is available starting from Python 3.7 version. The error message “fromisoformat: argument must be str” occurs when the argument passed to the “fromisoformat” method is not a string.

Here is an example that demonstrates the usage of “fromisoformat” method:

    
import datetime

date_str = "2022-03-15"
date_obj = datetime.datetime.fromisoformat(date_str)
print(date_obj)
    
  

In the above example, we have a string “2022-03-15” representing a date. We use the “fromisoformat” method to convert this string into a datetime object. The resulting datetime object is then printed, which will output “2022-03-15 00:00:00”.

It’s important to note that the argument passed to the “fromisoformat” method must strictly follow the ISO 8601 format, which includes the date in the “yyyy-mm-dd” format and an optional time in the “hh:mm:ss.ssssss” format. Any deviation from this format will result in a ValueError.

Similar post

Leave a comment