python 3.0, how to make print() output unicode?

6,298

Solution 1

The Windows command prompt (cmd.exe) cannot display the Unicode characters you are using, even though Python is handling it in a correct manner internally. You need to use IDLE, Cygwin, or another program that can display Unicode correctly.

See this thread for a full explanation: http://www.nabble.com/unable-to-print-Unicode-characters-in-Python-3-td21670662.html

Solution 2

You may want to try changing the environment variable "PYTHONIOENCODING" to "utf_8." I have written a page on my ordeal with this problem.

Solution 3

Check out the question and answer here, I think they have some valuable clues. Specifically, note the setdefaultencoding in the sys module, but also the fact that you probably shouldn't use it.

Solution 4

The problem of displaying Unicode charaters in Python in Windows is known. There is no official solution yet. The right thing to do is to use winapi function WriteConsoleW. It is nontrivial to build a working solution as there are other related issues. However, I have developed a package which tries to fix Python regarding this issue. See https://github.com/Drekin/win-unicode-console. You can also read there a deeper explanation of the problem. The package is also on pypi (https://pypi.python.org/pypi/win_unicode_console) and can be installed using pip.

Solution 5

Here's a dirty hack:

# works
import os
os.system("chcp 65001 &")
print("юникод")

However everything breaks it:

  • simple muting first line already breaks it:

    # doesn't work
    import os
    os.system("chcp 65001 >nul &")
    print("юникод")
    
  • checking for OS type breaks it:

    # doesn't work
    import os
    if os.name == "nt":
        os.system("chcp 65001 &")
    
    print("юникод")
    
  • it doesn't even works under if block:

    # doesn't work
    import os
    if os.name == "nt":
        os.system("chcp 65001 &")
        print("юникод")
    

But one can print with cmd's echo:

# works
import os
os.system("chcp 65001 & echo {0}".format("юникод"))

and here's a simple way to make this cross-platform:

# works

import os

def simple_cross_platrofm_print(obj):
    if os.name == "nt":
        os.system("chcp 65001 >nul & echo {0}".format(obj))
    else:
        print(obj)

simple_cross_platrofm_print("юникод")

but the window's echo trailing empty line can't be suppressed.

Share:
6,298
Sonu
Author by

Sonu

Updated on July 09, 2022

Comments

  • Sonu
    Sonu almost 2 years

    Image attached here

    I am creating a multi-line insert table using PHP, jquery, and AJAX. With this, the user can add more rows or remove rows from the table.

    Can anyone help me how to create a drop down in the column? I have 4 columns in the table and I want a drop-down on the item name. I am attaching the snap of the table. Please help me guys, I am a beginner.

        [<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Testing</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js">
    </head>
    <body>
        <br/><br/>
        <div class="container">
            <br/>
            <h2 align="center">Multiple Line Insert into MySql using Ajax and jQuery in PHP</h2>
            <br/>
            <div class="table-responsive">
                <table class="table table-bordered" id="crud_table">
                    <tr>
                        <th width="30%">Item Name</th>
                        <th width="10%">Item Code</th>
                        <th width="45%">Description</th>
                        <th width="10%">price</th>
                        <th width="5%"></th>
                    </tr>
                    <tr>
                        <td contenteditable="true" class="item_name"></td>
                        <td contenteditable="true" class="item_code"></td>
                        <td contenteditable="true" class="item_desc"></td>
                        <td contenteditable="true" class="item_price"></td>
                        <td></td>
                    </tr>
                </table>
                <div align="right">
                    <button type="button" name="add" id="add" class="btn btn-success btn-xs">+</button>
                </div>
                <div align="center">
                    <button type="button" name="save" id="save" class="btn btn-info">Save</button>
                </div>
                <br>
                <div id="inserted_item_data"></div>
            </div>
        </div>
    
    
    </body>
    </html>
    
    <script>
        $(document).ready(function(){
            var count =1;
            $('#add').click(function(){
                count = count + 1;
                var html_code = "<tr id='row"+count+"'>";
                html_code += "<td contenteditable='true' class='item_name'></td>";
                html_code += "<td contenteditable='true' class='item_code'></td>";
                html_code += "<td contenteditable='true' class='item_desc'></td>";
                html_code += "<td contenteditable='true' class='item_price'></td>";
                html_code += "<td><button type= 'button' name='remove' data-row='row"+count+" 'class= btn btn-danger btn-xs remove'>-</button></td>";
                html_code +="</tr>";
                $('#crud_table').append(html_code);
    
            });
            $(document).on('click','.remove', function(){
                var delete_row = $(this).data("row");
                $('#' + delete_row).remove();
    
            });
        });
    </script>][1]
    
    • Sean
      Sean about 6 years
    • Sonu
      Sonu about 6 years
      thanks for your answer but that is not what i want to know... have u seen the image i have attached? i have a table and i want drop down i the first coulmn.
    • Sean
      Sean about 6 years
      Yes I looked at your image. Do you want <td contenteditable="true" class="item_name"></td> to be a dropdown? Or do you just want to put a dropdown inside -> <td contenteditable="true" class="item_name"><select>....</select></td>? If the 1st, then use the link I suggested (you have to change the <p> to <td>. If the 2nd, not sure what your issue is.