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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:54:13+00:00 2026-06-09T04:54:13+00:00

A simple file like $ cat x.py x = u’Gen\xe8ve’ print x when run

  • 0

A simple file like

$ cat x.py
x = u'Gen\xe8ve'
print x

when run will give me:

$ python x.py
Genève

however, when run as a “command substitution” will give:

$ echo $(python x.py)
...
UnicodeEncodeError: 'ascii' codec...

I’ve tried with different terminal emulators (xterm, gnome-term) and the console on a ttyS. With bash and sh. With python2.4 and 2.7.
I’ve tried setting the LC_ALL or LANG to some utf-8 locale before running python.
I’ve checked the sys.getdefaultencoding().
And nothing helped.

The problem arises also when the script is called from another process (like java), but the above was the easiest way I found to replicate it.

I don’t understand what’s the difference between the two calls.
Can anyone help?

  • 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-06-09T04:54:14+00:00Added an answer on June 9, 2026 at 4:54 am

    The problem here is that in the second call you are basically writing to a pipe that only accepts bytestrings (file-like object). The same happens if you try to execute this:

    python x.py > my_file
    Traceback (most recent call last):
    File "x.py", line 2, in <module>
        print x
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 3: ordinal not in range(128)
    

    As the receiver only understands bytestrings and not unicode characters you must first encode the unicode string into a bytestring using the encode function:

    x = u'Gen\xe8ve'.encode('utf-8') 
    print x
    

    This will print the the unicode string encoded as a utf-8 bytestring (a sequence of bytes), allowing it to be written to a file-like object.

    $echo $(python x.py)
    Genève
    $python x.py 
    Genève
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to parse a simple, small XML file using python however work on
Take a simple XML file formatted like this: <Lists> <List> <Note/> ... <Note/> </List>
I have a simple assembly code file called exit.s that looks like the following:
A really simple shell script question. I have a file with something like this:
I am trying to write header file. I can write simple headers like add(int
I have a simple XML file like so: <?xml version=1.0 encoding=UTF-8?> <foo attr=blah &#176;
I cannot solve a very simple problem. My data file looks like: Crap Crap
I would like a regular expression, of which I will use with the Python
I need to create a diff file using standard UNIX diff command with python
I got a config/routes.rb file like this: map.resources :categories, :shallow => true do |cat|

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.