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

  • SEARCH
  • Home
  • 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 196363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:45:39+00:00 2026-05-11T16:45:39+00:00

I have a object which contains unicode data and I want to use that

  • 0

I have a object which contains unicode data and I want to use that in its representaion
e.g.

# -*- coding: utf-8 -*-

class A(object):

    def __unicode__(self):
        return u"©au"

    def __repr__(self):
        return unicode(self).encode("utf-8")

    __str__ = __repr__ 

a = A()


s1 = u"%s"%a # works
#s2 = u"%s"%[a] # gives unicode decode error
#s3 = u"%s"%unicode([a])  # gives unicode decode error

Now even if I return unicode from repr it still gives error
so question is how can I use a list of such objects and create another unicode string out of it?

platform details:

"""
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
'Linux-2.6.24-19-generic-i686-with-debian-lenny-sid'
""" 

also not sure why

print a # works
print unicode(a) # works
print [a] # works
print unicode([a]) # doesn't works 

python group answers that
http://groups.google.com/group/comp.lang.python/browse_thread/thread/bd7ced9e4017d8de/2e0b07c761604137?lnk=gst&q=unicode#2e0b07c761604137

  • 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-11T16:45:40+00:00Added an answer on May 11, 2026 at 4:45 pm

    s1 = u"%s"%a # works

    This works, because when dealing with ‘a’ it is using its unicode representation (i.e. the unicode method),

    when however you wrap it in a list such as ‘[a]’ … when you try to put that list in the string, what is being called is the unicode([a]) (which is the same as repr in the case of list), the string representation of the list, which will use ‘repr(a)’ to represent your item in its output. This will cause a problem since you are passing a ‘str’ object (a string of bytes) that contain the utf-8 encoded version of ‘a’, and when the string format is trying to embed that in your unicode string, it will try to convert it back to a unicode object using hte default encoding, i.e. ASCII. since ascii doesn’t have whatever character it’s trying to conver, it fails

    what you want to do would have to be done this way: u"%s" % repr([a]).decode('utf-8') assuming all your elements encode to utf-8 (or ascii, which is a utf-8 subset from unicode point of view).

    for a better solution (if you still want keep the string looking like a list str) you would have to use what was suggested previously, and use join, in something like this:

    u'[%s]' % u','.join(unicode(x) for x in [a,a])

    though this won’t take care of list containing list of your A objects.

    My explanation sounds terribly unclear, but I hope you can make some sense out of it.

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

Sidebar

Ask A Question

Stats

  • Questions 242k
  • Answers 242k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Paths are always relative to the initial script's location, even… May 13, 2026 at 7:39 am
  • Editorial Team
    Editorial Team added an answer The most readable would be, I believe, of the form:… May 13, 2026 at 7:39 am
  • Editorial Team
    Editorial Team added an answer I'd strongly suggest using one of the newer cousins of… May 13, 2026 at 7:39 am

Related Questions

I have a Java application that uses a C++ DLL via JNI. A few
I have following model: class UserProfile(models.Model): User profile model, cintains a Foreign Key, which
I get from a NSURLConnection a NSData object which I convert with [[NSMutableString alloc]
I have a Request object which contains a list of Approvers. An approver has

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.