from datetime import datetime

def str2datetime(s):
    parts = s.split('.')
dt = datetime.strptime(parts[0], '%Y-%m-%d %H:%M:%S')
return dt.replace(microsecond=int(parts[1]))
Parsing datetime strings with microseconds