Python string formatting UnicodeEncodingError
<p>I'm writing a Python script that reads tweets and inserts them into MySQL. Depending on the attributes of each tweet, I need to insert different fields. For that reason, I'm building the fields and values section of the query string as I go, using Python string formatting for convenience:</p>

<pre><code>values = """%s, %s, '%s','%s','%s','%s',%s,'%s','%s','%s'""" % (
                url_id, tweet['from_user_id'], conn.escape_string(tweet['location']),
                conn.escape_string(tweet['profile_image_url']),
                tweet['created_at'], tweet['from_user'], tweet['id'],
                conn.escape_string(tweet['text']),
                conn.escape_string(tweet['iso_language_code']), conn.escape_string(tweet['source'])
            )
</code></pre>

<p>When I do this with tweets that have UTF8 characters, though, I get an error like this:</p>

<pre><code>values = """%s, %s, '%s','%s','%s','%s',%s,'%s','%s','%s'""" % (
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 117: ordinal not in range(128)
</code></pre>

<p>I think that the format string (the one with all the "%s"s) is interpreted as ASCII by default, and that's clashing with the UTF-8 characters. I need to keep everything in UTF-8, since this code has to work with any possible language.</p>

<p>So how do I specify that the formatting string is UTF-8? I thought I could change the default encoding for the entire script, but I'm using Python 2.4 and sys.setdefaultencoding doesn't exist in that version. Right now, I'm just not sure how to do that, or if that's even the right thing to do.</p>
