我在一个问题中有两个String.printable的谜团。

首先,在Python 2.6中:

>>> string.printable
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'

看看字符串的结尾,你会发现'\ x0b \ x0c'像拇指一样伸出来。他们为什么在那里?我使用的机器设置为澳大利亚设置,所以不应该有任何重音字符等。

接下来,尝试运行此代码:

for x in string.printable: print x,
print
for x in string.printable: print x

第一行成功打印由空格分隔的所有字符。两个奇怪的字符变成了男性和女性的符号。

第二行成功打印除了换行符之外的所有字符。男性符号打印;女性符号被替换为缺失的字符(方框)。

我确信Python并不是出于性别偏见,所以有什么不同呢?

有帮助吗?

解决方案

“可打印”有所不同。用于“可以在屏幕上显示”。您的终端显示低ascii打印机控制代码0x0B和0x0C作为男性和女性符号,因为这是您的字体中的那些索引包含的。这些字符更准确地描述为Vertical Tabulator和Form Feed字符。这两个字符以及\ t \ r和\ n都是可打印的,并且在打印机上做了很好的定义。

其他提示

在cmd.exe中:

>>> print '\x0b'
♂
>>> print '\x0c'
♀
>>> print '\f' # form feed
♀
>>> print '\v' # vertical tab
♂
>>>

内部Emacs:

>>> print '\f\v'
^L^K

以下是格式(5)的摘录'手册页:

| Sequence | Character    | Terminal Action                             |
|----------+--------------+---------------------------------------------|
| \f       | form-feed    | Moves the printing position to the initial  |
|          |              | printing position of the next logical page. |
| \v       | vertical-tab | Moves the printing position to the start of |
|          |              | the next vertical tab position. If there    |
|          |              | are no more vertical tab positions left on  |
|          |              | the page, the behavior is undefined.        |
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top