0

I have a list in the following format which i want to sort by date. I tried to convert the date-string into datetime-objects but it always says that it doesnt match the format. liste = [['25.03.19', 41, 129], ['19.03.19', 47, 95], ...]* error: ValueError: time data '' does not match format '%d.%m.%y'

def go(path):
    liste = []
    for data in (os.listdir(path)):
       datei = path + "/" + data
       datenliste = data_as_list(datei)
       liste.append(stats(datenliste))
    print(liste)*
    liste.sort(key=lambda date: datetime.datetime.strptime(date[0], '%d.%m.%y'))

The stats() funktion brings the datenliste in the special format [date, integer, integer]

  • 2
    The error message says that one of the 'dates' in your list is actually an empty string. Take a closer look at your data. – BoarGules Sep 22 '20 at 06:39
  • what are your imports, what is `data_as_list`, what is `stats`? please don't let the readers of your question guess about those things. – FObersteiner Sep 22 '20 at 06:42
  • I think this is the proper way to do it. https://stackoverflow.com/questions/466345/converting-string-into-datetime – BeeFriedman Sep 22 '20 at 06:47
  • Im sorry. The data_as_list() function goes through a textfile and adds every line of the file into a list. The stats() function goes through the this created list and filters out the information that you get this format: [date(string), integer, integer] – jonaskoennel Sep 22 '20 at 08:53
  • The Value Error sounded very clear but i couldnt find an empty sting in my list. I wrote i script after your advice that goes through the list and it found one empty string. Thanks for your help @BoarGules and all others – jonaskoennel Sep 22 '20 at 09:10
  • @jonaskoennel: well thanks for the update but that info should be part of the question - as it stands this seems pretty hard to debug since the error might originate from somewhere else than the code you show. The `strptime` format code is absolutely correct for a string like `'25.03.19'`. – FObersteiner Sep 22 '20 at 11:28

0 Answers0