-1

Hi I am trying to parse date from a string using dateutil parser. But I am getting this error. Does the library accept python 'str' or 'unicode'? Does the string need any conversion? Tried with that, but no luck.

enter image description here

Sachin AB
  • 1
  • 1
  • 1

1 Answers1

0

i would put all the digit numbers before the date/time stamp in your message AFTER the date/time stamp, then that portion would be separated as a tuple.

fuzzy_with_tokens – If True, fuzzy is automatically set to True, and the parser will return a tuple where the first element is the parsed datetime.datetime datetimestamp and the second element is a tuple containing the portions of the string which were ignored:

Another alternative is simply seeking out the known date format using regex. Of course, this varies by use case

from dateutil.parser import parse
import re

date_pattern = re.compile('\d{2}\d{2}\d{2}')

def extract_date(filename):
    matches = re.match(date_pattern, filename)
    if matches:
        return parse(matches.group(0))
    else:
        return None

extract_date('30\09\17.jpg')
Clay Chester
  • 91
  • 16