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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:41:39+00:00 2026-05-24T12:41:39+00:00

In my Python script running at the command line on Ubuntu, it’s selecting UTF8-encoded

  • 0

In my Python script running at the command line on Ubuntu, it’s selecting UTF8-encoded content from a MySQL database.

Then, I want to print the string to the console.

The displayed strings have an encoding problem, as they don’t show the accented characters correctly. How do I fix this?

Preferably, the script would make the decision, rather than setting a system environment, to ensure easy running on other systems.

  • 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-24T12:41:40+00:00Added an answer on May 24, 2026 at 12:41 pm

    It is very strongly recommended that you not use “?” as a replacement char. Just set your output encoding to UTF-8 and be done with it.

    for s in ("stdin","stdout","stderr"): 
       setattr(sys, s, io.TextIOWrapper(getattr(sys, s).detach(), encoding="utf8"))
    

    Alternately, set your PYTHONIOENCODING envariable to utf8 so that python stops guessing about the output encoding.

    Either approach is infinitely much better than manually encoding, which is stupid.

    If you refuse to upgrade to Python3, I also recommend

    from __future__ import unicode_literals
    

    to banish all that stupid u'...' stuff.

    Lately I’ve starting all my Python progams like this:

    #!/usr/bin/env python3.2
    # -*- coding: UTF-8 -*-
    
    from __future__ import print_function
    from __future__ import unicode_literals
    
    import re
    import sys
    import os
    
    if not (("PYTHONIOENCODING" in os.environ)
                and
            re.search("^utf-?8$", os.environ["PYTHONIOENCODING"], re.I)):
        sys.stderr.write(sys.argv[0] + ": Please set your PYTHONIOENCODING envariable to utf8\n")
        sys.exit(1)
    
    import unicodedata
    if unicodedata.unidata_version < "6.0.0":
        print("WARNING: Your old UCD is out of date, expected at least 6.0.0 but got", 
               unicodedata.unidata_version)
    
    wide_enough = (sys.maxunicode >= 0x10FFFF)
    if not wide_enough:
        print("WARNING: Narrow build detected, your Python lacks full Unicode support!!")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to prevent multiple instances of the same long-running python command-line script from
I have a long-running Python script that I run from the command-line. The script
I am running command-line Python scripts from the Windows taskbar by having a shortcut
I'm running this command in a Python script: try: print sql_string cursor.execute(sql_string) except: print
I'm running a script from crontab that will just ssh and run a command
Receiving a segfault when running this very short script in Ubuntu. from osgeo import
I have a Python application (command line tool running on a machine M1) that
I have a program that is run from the command line like this python
I have a Python script that is running a few ls commands. This script
A python script is running two parallel python processes ( created via os.fork() )

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.