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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:40:38+00:00 2026-05-25T11:40:38+00:00

I have a python program running very well. It connects to several websites and

  • 0

I have a python program running very well. It connects to several websites and outputs the desired information. Since not all websites are encoded with utf-8, I am requesting the charset from the headers and using unicode(string, encoding) method to decode (I am not sure whether its the appropriate way to do this but it works pretty well). When I run the python program I receive no ??? marks and it works fine. But when I run the program using php’s system function, I receive this error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u0131' in position 41: ordinal not in range(128)

This is a python specific error but what confuses me is that I don’t receive this error when I run the program using the terminal. I only receive this when I use php’s system function and call the program from php. What may be the cause behind this problem?

Here is a sample code:

php code that calls python program:

system("python somefile.py $search") // where $search is the variable coming from an input

python code:

encoding = "iso-8859-9"
l = "some string here with latin characters"
print unicode("<div class='line'>%s</div>" % l, encoding)
# when I run this code from terminal it works perfect and I receive no ??? marks
# when I run this code from php, I receive the error above
  • 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-25T11:40:38+00:00Added an answer on May 25, 2026 at 11:40 am

    From the PrintFails wiki:

    When Python finds its output attached to a terminal, it sets the
    sys.stdout.encoding attribute to the terminal’s encoding. The print
    statement’s handler will automatically encode unicode arguments into
    str output.

    This is why your program works when called from the terminal.

    When Python does not detect the desired character set of the
    output, it sets sys.stdout.encoding to None, and print will invoke the
    “ascii” codec.

    This is why your program fails when called from php.
    To make it work when called from php, you need to make explicit what encoding print should use. For example, to make explicit that you want the output encoded in utf-8 (when not attached to a terminal):

    ENCODING = sys.stdout.encoding if sys.stdout.encoding else 'utf-8'
    print unicode("<div class='line'>%s</div>" % l, encoding).encode(ENCODING)
    

    Alternatively, you could set the PYTHONIOENCODING environment variable.
    Then your code should work without changes (both from the terminal and when called from php).

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

Sidebar

Related Questions

I have a Python program running a thread that consistently outputs the following: (my_program.py:12313):
Is it possible to find any information about what a Python program running right
I'm interested in running a Python program using a computer cluster. I have in
I have a Python program that uses the threading module. Once every second, my
I have a python program, which runs on the CPython implementation, and inside it
I'd like to have a python program alert me when it has completed its
I have a Python script that calls an executable program with various arguments (in
I have a Python script that needs to execute an external program, but for
I have embedded a Python interpreter in a C program. Suppose the C program
I have a python program that does something like this: Read a row from

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.