简体   繁体   English

如何制作python 3 print('\\ ufeff')

[英]How to make python 3 print('\ufeff')

>>> print('\ufeff')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'gbk' codec can't encode character '\ufeff' in position 0: illegal multibyte sequence

I know 我知道

>>> stdout = open(1, 'w', encoding='gb2312', errors='ignore')
>>> print('\ufeff', file=stdout)

or 要么

>>> print(repr('\ufeff'))
'\ufeff'

but too long,What else I can finish it simple 但是太长了,还有什么我可以简单完成的

英语真难写,有木有?这坛子有国人么?帮老弟一帮啊。。 英语真难写,有木有?这坛子有国人么?帮老弟一帮啊。。

The '\' is unprintable Unicode character with special meaning. '\\ ufeff'是不可打印的Unicode字符,具有特殊含义。 It is used as the UTF-16 BOM (Byte Order Mark) to detect the order of bytes stored in memory (later written to a file) when two-byte integers are used. 当使用两个字节的整数时,它用作UTF-16 BOM(字节顺序标记)来检测存储在内存中的字节顺序(后来写入文件)。 When found at the begining of the file, it should help only to detect the way the hardware stores the small integers, and then it should be ignored. 在文件开头找到该文件时,它仅应有助于检测硬件存储小整数的方式,然后应将其忽略。

Have a look at http://en.wikipedia.org/wiki/Byte_order_mark for more details. 有关更多详细信息,请访问http://en.wikipedia.org/wiki/Byte_order_mark

You seem to be trying to print a Unicode character to a terminal which do not support that character. 您似乎正在尝试将Unicode字符打印到不支持该字符的终端。 Doing so is essentially impossible. 这样做基本上是不可能的。 It may also be that the character in question should be a part of the GBK encoding, but that the Python implementation has a bug. 也可能是所讨论的字符应该是GBK编码的一部分,但是Python实现存在一个错误。

Your first solution where you open stdout using gb2312 indicates that the terminal itself do support the character if you just change it's encoding. 在第一个使用gb2312打开stdout的解决方案中,如果您只是更改字符的编码,则表明终端本身确实支持该字符。 That should be doable as a setting in the operating system somehow. 作为操作系统中的设置,应该可以做到这一点。 That's probably the best solution for you. 这可能是最适合您的解决方案。 If you can, then switch to UTF-8 or UTF-16. 如果可以,请切换到UTF-8或UTF-16。 They should support all Unicode characters. 它们应支持所有Unicode字符。

Otherwise all you can do is to try to filter the character out of what you are printing before you print it, or encode it to binary with errors='ignore' or errors='replace' . 否则,您所能做的就是尝试在打印之前从打印内容中过滤出字符,或者使用errors='ignore'errors='replace'其编码为二进制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM