Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6348607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:27:14+00:00 2026-05-24T21:27:14+00:00

Background I’m in a real mess with unicode and Python. It seems to be

  • 0

Background

I’m in a real mess with unicode and Python. It seems to be a common angst and I’ve tried using other solutions out there but I just can’t get my head around it.

Setup

MySQL Database Setup

  • collation_database: utf8_general_ci
  • character_set_database: utf8

SQLAlchemy Model

class Product(Base):
    id = Column('product_id', Integer, primary_key=True)
    name = Column('product_name', String(64)) #Tried using Unicode() but didn't help

Pyramid View

@view_config(renderer='json', route_name='products_search')
def products_search(request):
    json_products = []
    term = "%%%s%%" % request.params['term']

    products = dbsession.query(Product).filter(Product.name.like(term)).all()

    for prod in products:
        json_prod = {'id': prod.id, 'label': prod.name, 'value': prod.name, 'sku': prod.sku, 'price': str(prod.price[0].price)}
        json_products.append(json_prod)

    return json_products

Problem

I get encoding errors reported from the json module (which is called as its the renderer for this route) like so:

UnicodeDecodeError: 'utf8' codec can't decode byte 0x96 in position 37: invalid start byte

The culprit is a “-” (dash symbol) in the prod.name value. Full stack trace here. If the returned products don’t have a “-” in then it all works fine!

Tried

I’ve tried encoding, decoding with various types before returning the json_products variable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:27:15+00:00Added an answer on May 24, 2026 at 9:27 pm

    The above comment is right, but more specifially, you can replace 'label': prod.name with 'label': prod.name.decode("cp1252"). You should probably also do this for all of the strings in your json_prod dictionary since it’s likely you’ll see cp1252 encoded characters elsewhere in the real-world use of your application.

    On that note, depending on the source of these strings and how widely that source is used in your app, you may run into such problems elsewhere in your app and generally when you least expect it. To investigate further you may want to figure What the source for these strings is and if you can do the decoding/re-encoding at a lower level to correct most future problems with this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[background below] I've got my data modelled out in SQLObject in Python on the
Background: At my company we are developing a bunch applications that are using the
Background I am writing and using a very simple CGI-based (Perl) content management tool
Background School Corp. has two databases with a common table, employees. These two databases
Background: I know, via pypi, about DOAP . Is there something similar to describe
BACKGROUND I am automating an PowerPoint 2007 via C# I am writing unittests using
Background: I am using Eclipse to develop an Android app. I have an xml
Background: I'm in the process of creating a web service using ASP.NET 2.0. This
Background: One of the problems with using a local static variable in a function
Background: I want to check-out the source code from Cliche , which is stored

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.