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 7672863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:22:55+00:00 2026-05-31T16:22:55+00:00

I am dealing with some unicode strings, which I am encoding using utf-8 whenever

  • 0

I am dealing with some unicode strings, which I am encoding using utf-8 whenever I need to display them. This way I make sure that, even when redirecting the output of my script to a file, the proper encoding is used (I know there are other ways to do this, but this is not the point).

Now, sometimes I need to tabulate some data, and for that I use format specifiers, as shown below:

def tabulate(uni1, uni2):
    print "%-15s,%-15s" % (uni1.encode('utf-8'), uni2.encode('utf-8'))

print '01234567890123456789' # ruler
tabulate(u'HELLO', u'BYE')
tabulate(u'ñññññ', u'BYE')

This program will produce the following output

01234567890123456789
HELLO          ,BYE            
ñññññ     ,BYE

As you can see, the second string is not properly tabulated. I guess that %s is not aware of the encoding of the string, and computes badly its length.

Is there a solution to this problem?

  • 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-31T16:22:56+00:00Added an answer on May 31, 2026 at 4:22 pm

    Here is an implementation for what Ignacio pointed out, which is to do the formatting before the encoding:

    def tabulate(uni1, uni2):
        print (u"%-15s,%-15s" % (uni1, uni2)).encode('utf-8')
    
    >>> tabulate(u'HELLO', u'BYE')
    HELLO          ,BYE            
    >>> tabulate(u'ñññññ', u'BYE')
    ñññññ          ,BYE    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am dealing with some generated spans and I would like to find which
I need some help dealing with three Threads in Android One thread is the
I'm dealing with some very non-uniform data and I'm using Ruby regular expressions to
I believe I may be dealing with some view issues which are not allowing
I'm dealing with some quite large files which are uncomfortable to upload via http,
I need some help dealing with timeouts in a Grails app. Context: In my
I'm dealing with some problems in a few files about the encoding. We receive
I'm dealing with some legacy systems that are using RS232 to communicate with peripherals.
I'm having trouble dealing with some XML file (which is at the end of
I am dealing with some auto-generated XSLT code. It contains the following: string(string(.)) number(string(.))

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.