python - 为什么打印这些值会在不同的操作系统和版本中给出不同的值?
问题描述
解决方案
It is a question of encoding.
In Latin1 or Windows 1252 encoding, you have:
0xef -> ï (LATIN SMALL LETTER I WITH DIAERESIS)
0xbe -> ¾ (VULGAR FRACTION THREE QUARTERS)
0xad -> undefined and non printed in your examples
0xde -> Þ (LATIN CAPITAL LETTER THORN)
In utf-8 encoding, you have:
'\xef\xbe\xad'
-> u'\uffad'
or 'ᆳ'
(HALFWIDTH HANGUL LETTER RIEUL-SIOS)
'\xde'
-> should raise an UnicodeDecodeError...
In Windows, Python2 or Python3 both use Windows 1252 code page (in your example). On Kali, Python2 sees the string as byte string and the terminal displays it in utf8, while Python3 assumes it already contains unicode character values and displays them directly.
As in Latin1 (and in Windows 1252 for all characters outside 0x80-0x9f) the byte code is the unicode value, that is enough to explain your outputs.
What to learn: be explicit whether strings contains unicode or bytes and beware of encodings!
推荐阅读
- python-3.x - 在 CSV 中保留或不保留标题以进行训练
- pandas - 对 pandas 中的两列进行分组,并对每组的总计执行操作
- html - html元素上的文本阴影效果
- python - sys.exit() 和 pygame.quit() 和 os._exit(0) 无法关闭 pygame 显示窗口
- python - 如何在一行中快速将 DataFrame 结果导出到 Excel 文件
- excel - 比较不同工作表中的两个表
- tensorflow - How to implement tensorflow cosine_decay
- java - How to Call "public void paint(Graphics g)" with Parameters (Canvas)
- javascript - 如何在 Jmeter Webdriver Sampler 中禁用“Chrome --disable-web-security”?
- python - Is it possible to shorten several layers of 'for in' with actions at each layer?