Split a string in parts and convert this in a datetime
<p>I scrapped a html string with the following content.</p>

<blockquote>
  <p>[u'Mitglied seit M\xe4rz 2016']</p>
</blockquote>

<p>M\xe4rz should be MÃ¤rz (German word for March).</p>

<p>I want to convert this scrapped output in a datetime.
My first try was to convert the output in a string and split this with the following code.</p>

<pre><code>strDate = string.split(str(scrapped))
</code></pre>

<p>My new output is now:</p>

<blockquote>
  <p>["[u'Mitglied", 'seit', 'M\xe4rz', "2016']"]</p>
</blockquote>

<p>The next step will be to add the first day of the month in the string.</p>

<pre><code>&gt; strDate = "1. " + strDate[2] + " " + strDate[3]
</code></pre>

<p>New Output is:</p>

<blockquote>
  <p>"1. M\xe4rz 2016']"</p>
</blockquote>

<p>How can I delete the \xe4 in a Ã¤ and delete the ']. 
And finally how can I convert this string "1. MÃ¤rz 2016" in a datetime with Python.</p>

<p>Thanks for your answers.</p>

<p>There is a lot of code. You can simplify or adopt it, but I think it should help:</p>

<pre><code># encoding: utf-8

import datetime

months = {
    u'Januar': '1',
    u'Februar': '2',
    u'MÃ¤rz': '3',
    u'April': '4',
    u'Mai': '5',
    u'Juni': '6',
    u'Juli': '7',
    u'August': '8',
    u'September': '9',
    u'Oktober': '10',
    u'November': '11',
    u'Dezember': '12'
}

def str2date(str_date, date_format='%d.%m.%Y', err_value=None, do_raise=False):
    u"""
        Convert string with date to datetime instance
    """
    if isinstance(str_date, datetime.date):
        result = str_date
    else:
        datetime_templates = (
            (date_format, 255),
            ('%d.%m.%Y', 10),
            ('%Y-%m-%dT%H:%M:%S', 19),
            ('%Y-%m-%d %H:%M:%S', 19),
            ('%d.%m.%Y %H:%M:%S', 19),
            ('%Y-%m-%dT%H:%M', 16),
            ('%Y-%m-%d %H:%M', 16),
            ('%d.%m.%Y %H:%M', 16),
            ('%Y-%m-%d', 10),
            ('%H:%M:%S', 8),
            ('%H:%M', 5),
        )

        for tmpl, baund in datetime_templates:
            try:
                result_date = datetime.datetime.strptime(str_date[:baund], tmpl).date()
            except (ValueError, TypeError) as err:
                raise err
            else:
                result = result_date
                break
        else:
            if do_raise:
                raise ValueError
            else:
                result = err_value
    return result

scrapped = "[u'Mitglied seit M\xe4rz 2016']"

encoded = unicode(scrapped.replace("[u'", '').replace("']", ''), 'unicode-escape')

splitted = encoded.split()

replaced = [months[i] if i in months else i for i in splitted]

str_date = u'.'.join(['1', replaced[2], replaced[3]])

result_date = str2date(str_date)

print result_date
print isinstance(result_date, datetime.date)
</code></pre>

python datetime scrapy type-conversion