(Nov-04-2020, 06:02 PM)DeaD_EyE Wrote: [ -> ] (Nov-04-2020, 05:52 PM)beginner2020 Wrote: [ -> ]I got the below error, using python version 2.7.5. Will that be due to version.
date_str, _ = line.split(maxsplit=1)
TypeError: split() takes no keyword arguments
I saw it too late.
You should avoid the use of Python 2.7. It's end of life.
Python 3.6 is the oldest available version which has still support.
Python 3.5 has also reached the end of life.
That maxsplit can be a keyword-argument was introduced with Python 3.3.
Start your repl or script with python3
.
Just python
without a 3 will start the Python 2.7 interpreter on the most distributions.
Hi
Thanks for the suggestion. Changed the environment to python 3.6 .Executed the code and got the below error.
date_str, _ = line.split(maxsplit=1)
ValueError: not enough values to unpack (expected 2, got 0)
Not able to figure the error. Could you please advise.
did you check your file for empty lines? Check that you don't have empty lines at the end of the data
(Nov-04-2020, 06:28 PM)beginner2020 Wrote: [ -> ]Not able to figure the error. Could you please advise.
Use the parse_datetime where the try: .. except .. is included:
def parse_datetime(line):
date_fmt = "%Y/%m/%d:%H:%M:%S"
try:
date_str, _ = line.split(maxsplit=1) # <- this method could raise a ValueError (empty line e.g.)
return datetime.strptime(date_str, date_fmt) # <- if date_str is invalid datetime raises a ValueError
except ValueError: # <- here the ValueError Exception is catched
return datetime.min # -> retunring datetime(1,1,1,0,0)
From my previous post:
Quote:The parse_date was made twice to show:
- How to split tasks into smaller easier tasks -> better for testing. For example, you can use this function to test each line. No file for testing needed at all.
- How to catch Exceptions, handle them, retuning a default value for sorting.
Thanks All. This issue is resolved.