擅长:python、mysql、java
<p>此日期字符串位于<a href="http://tools.ietf.org/html/rfc2822.html#page-14" rel="nofollow">RFC 2822 date format</a>中,可以使用<a href="http://docs.python.org/2/library/email.util.html#email.utils.parsedate" rel="nofollow">email.utils.parsedate</a>(标准库的一部分)进行分析:</p>
<pre><code>In [428]: import email.utils as eu
In [429]: eu.parsedate('Thu, 14 Mar 2013 13:33:07 -0400')
Out[429]: (2013, 3, 14, 13, 33, 7, 0, 1, -1)
</code></pre>
<p>一旦有了datetime对象,就可以将其以该格式(不带格式)插入到MySQL中:</p>
<pre><code>date = eu.parsedate('Thu, 14 Mar 2013 13:33:07 -0400')
self.cursor.execute("INSERT INTO table1 (`DATE`) VALUES (%s)",(date,))
</code></pre>
<hr/>
<p>注意:<code>cursor.execute</code>的第二个参数必须是序列。所以使用元组<code>(date,)</code>,而不是datetime对象<code>(date)</code>。</p>
<p>此外,在SQL字符串中不需要分号。</p>