Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python sort date
#11
(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.
Reply
#12
did you check your file for empty lines? Check that you don't have empty lines at the end of the data
beginner2020 likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#13
(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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#14
Thanks All. This issue is resolved.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 251 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 623 Jan-20-2024, 04:45 AM
Last Post: 1418
  How to see the date of installation of python modules. newbieAuggie2019 4 1,640 Mar-31-2023, 12:40 PM
Last Post: newbieAuggie2019
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,328 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  Date format and past date check function Turtle 5 4,281 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  How to sort values descending from a row in a dataframe using python sankarachari 1 1,427 Aug-16-2021, 08:55 AM
Last Post: jamesaarr
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 2,245 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
  How to add date and years(integer) to get a date NG0824 4 2,891 Sep-03-2020, 02:25 PM
Last Post: NG0824
  What's the best way to sort this data with Python? xtrax 2 1,702 Mar-15-2020, 08:08 AM
Last Post: Larz60+
  Date from from excel to Python. atp_13 1 1,780 Nov-24-2019, 12:26 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020