Python 2.7 based on Sublime Text 3 does not print the character '\ uFFFD'

Problem.

I am using Python 2.7 on Sublime Text 3 and have a print problem.
In some cases, I get pretty confusing output for '\uFFFD'- 'REPLACEMENT CHARACTER'.


For instance:

print u'\ufffd' # should be ' ' - the 'REPLACEMENT CHARACTER'
print u'\u0061' # should be 'a'
-----------------------------------------------------
[Finished in 0.1s]

After inversion of order:

print u'\u0061' 
print u'\ufffd'
-----------------------------------------------------
a
 
[Finished in 0.1s]

So, Sublime can print the "" character, but for some reason does not do this in the first case.
And the dependence of the output on the order of utterances seems rather strange.


The problem with replacing char leads to very unpredictable print quality in general.
For example, I want to print decoded bytes with error substitution:

cp1251_bytes = '\xe4\xe0' # '' in cp1251 
print cp1251_bytes.decode('utf-8', errors='replace')
-----------------------------------------------------
  
[Finished in 0.1s]

Let me replace the bytes:

cp1251_bytes = '\xed\xe5\xf2' # '' in cp1251
print cp1251_bytes.decode('utf-8', errors='replace')
-----------------------------------------------------
[Finished in 0.1s]

And add another print statement:

cp1251_bytes = '\xed\xe5\xf2' # '' in cp1251 
print cp1251_bytes.decode('cp1251') 
print cp1251_bytes.decode('utf-8', errors='replace')
-----------------------------------------------------

   
[Finished in 0.1s]

The following is an illustration of the implementation of some other test cases:

enter image description here


To summarize , in the described print mode there are the following templates:

it depends on the even / odd number of characters '\uFFFD'in the print instruction it depends on the print order it depends on the particular assembly

My questions:

Why is this happening? How to fix the problem?


My Python 2.7 sublime-build file:

{   
    "cmd": ["C:\\_Anaconda3\\envs\\python27\\python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",
    "env": {"PYTHONIOENCODING": "utf-8"}
}

With Python 2.7 installed separately from Anaconda, the behavior is exactly the same.

+4
source share
2 answers

I reproduced your problem and I found a solution that works on my platform anyway: Remove the flag -ufrom your configuration configurationcmd .

100%, , , , - , , . :

  • -u Python
  • . , "あ" (U + 3042).
  • . "env": {"PYTHONIOENCODING": "utf-16be"} print u'\u3042' 0B.

, UTF-16BE, , , . , . 0x30. , UTF-16BE ASCII , , 0. B.

UTF-8 , ASCII, , , .

+1

-1 - UTF8

, . config

{   
    "cmd": ["F:\\Python27-14\\python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",
    "env": {
        "PYTHONIOENCODING": "utf_8_sig"
    },
}

.

build options

correct conclusion

, Python 2.7 Sublime. , # -*- coding: utf-8 -*- .

# -*- coding: utf-8 -*-

print u'\u0061' # should be 'a'
print u'\ufffd' # should be ' ' - the 'REPLACEMENT CHARACTER'

print 1

print 2

unicode python?

# -*- coding: utf-8 -*-, Python, , , utf-8. Python 2 ASCII ( Python 3 it utf-8). , .

+1

Source: https://habr.com/ru/post/1687916/


All Articles