0

I am having a typical utf-8 encoding issue, but so far I haven't been able to resolve it.

class StatsTandE(webapp2.RequestHandler):
  def get(self):

    conn = rdbms.connect(instance=_INSTANCE_NAME, database='origami')
    cursor = conn.cursor()
    cursor.execute('SELECT ui.displayName, ui.title, ui.costCenter FROM views AS v')
    results = [[str(row[0]), str(row[1]), str(row[2])] for row in cursor.fetchall()]
    logging.info('results identified as %s', results)

    template_file_name = 'templates/stats_results.html'
    template_values = {
      'results': results,
    }

    path = os.path.join(os.path.dirname(__file__), template_file_name)
    self.response.out.write(template.render(path, template_values))
    conn.close()

The error I am seeing is:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 4: ordinal not in range(128)

Changing str(row[0]) to str(row[0]).encode('utf-8') didn't work...

Any suggestions?

4
  • Which version of python are you using ? Unicode issues are resolved in different way whether you are using python 2.X or python 3. Commented Jan 16, 2013 at 21:40
  • If the string is going to contain UTF-8 characters, you should use the unicode datatype in Python 2. Commented Jan 16, 2013 at 21:42
  • We are using Python 2.7. How would I set the unicode datatype? Commented Jan 16, 2013 at 21:49
  • 2
    what happens if you just remove all str calls? General rule for data that represents text: convert from bytes to Unicode as soon as possible on input, use Unicode everywhere inside your program, convert to bytes from Unicode as late as possible on output. Commented Jan 16, 2013 at 22:02

1 Answer 1

1

Try changing str(row[0]) to unicode(row[0]).

Update: As others have stated: you should not even be using unicode and str unless you need it. Try just row[0], row[1], row[2]. When you need to display it, do something like this: row[0].encode('utf8').

Sign up to request clarification or add additional context in comments.

1 Comment

That worked! However, how would I then convert the unicode once more into a string so that it displays nicely on the page? Would I decode it once more?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.