Python 一些转义序列不起作用
Python some escape sequences do not work
我测试了一些转义序列。他们中的大多数工作,但其中一些没有。这是为什么?
这是我为测试转义序列而编写的代码:
# -*- coding: UTF-8 -*-
tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split \non a line."
backslash_cat = "I'm \ a \ cat."
fat_cat = '''
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
'''
print tabby_cat
print persian_cat
print backslash_cat
print fat_cat
#while True:
# for i in ["/", "-", "|", "\", "|"]:
# print "%s\r" % i,
print "This should ring a bell \a"
print "Does this have a backspace\b"
print "Printing \"double quotes\" and some \'single quotes\'"
print "Formfeed.\fWill this work?"
print "Linefeed.\nWill this work?"
print "Print the character named name: \N{name}"
print "Print a carriage return.\rWill this work?"
print "A 16 bit unicode character: \u6C34"
print "Let's check this vertical tab: \v\vWhat is this?"
print "This should output an octal value: 1"
print "This should output a hex value: \xAA"
终端输出:
I'm tabbed in.
I'm split
on a line.
I'm \ a \ cat.
I'll do a list:
* Cat food
* Fishies
* Catnip
* Grass
This should ring a bell
Does this have a backspace
Printing "double quotes" and some 'single quotes'
Formfeed.
Will this work?
Linefeed.
Will this work?
Print the character named name: \N{name}
Will this work?e return.
A 16 bit unicode character: \u6C34
Let's check this vertical tab:
What is this?
This should output an octal value: I
This should output a hex value: �
不起作用的是:
- 铃铛:
\a
- 退格键:
\b
- 角色名称:
\N{name}
(已解决)
- 16 位和 32 位 unicode 字符:
\u6C34
(已解决)
我应该怎么做才能使这些转义序列起作用?
================编辑=====================
print u"Print the character named name: \N{BLACK SPADE SUIT}"
print u"A 16 bit unicode character: \u6C34"
Print the character named name: ♠
A 16 bit unicode character: 水
- 16 位和 32 位 unicode 字符:
\u6C34
- 角色名:
\N{name}
当在字符串前面加上 u
时,这些工作得很好
\a
和 \b
序列工作得很好,但您的 终端 可能会随意忽略它们。这些序列只是创建十六进制值分别为 07 和 08 的字节的别名:
>>> '\a'
'\x07'
>>> '\b'
'\x08'
两个序列各产生一个字节。由接收终端将字节解释为控制字符,终端可以自由地忽略该字节或对其执行不同的操作。
一些终端会闪烁而不是响铃;这通常是可配置的。在我的终端上 \b
可以很好地移动光标;它不会删除前面的字符。当您在其后添加更多文本时,位置的变化就会变得可见:
>>> print 'abc\b'
abc
>>> print 'abc\bd'
abd
d
覆盖了 c
字符,因为退格字符将光标位置移回了一个位置。
Unicode 转义序列仅适用于 Unicode 字符串,使用 u'...'
文字:
>>> print "A 16 bit unicode character: \u6C34"
A 16 bit unicode character: \u6C34
>>> print u"A 16 bit unicode character: \u6C34"
A 16 bit unicode character: 水
>>> print u"Print the character named: \N{CJK UNIFIED IDEOGRAPH-6C34}"
Print the character named: 水
请注意,它还取决于您的终端(控制台)是否真的支持打印的 Unicode 代码点!
我测试了一些转义序列。他们中的大多数工作,但其中一些没有。这是为什么?
这是我为测试转义序列而编写的代码:
# -*- coding: UTF-8 -*-
tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split \non a line."
backslash_cat = "I'm \ a \ cat."
fat_cat = '''
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
'''
print tabby_cat
print persian_cat
print backslash_cat
print fat_cat
#while True:
# for i in ["/", "-", "|", "\", "|"]:
# print "%s\r" % i,
print "This should ring a bell \a"
print "Does this have a backspace\b"
print "Printing \"double quotes\" and some \'single quotes\'"
print "Formfeed.\fWill this work?"
print "Linefeed.\nWill this work?"
print "Print the character named name: \N{name}"
print "Print a carriage return.\rWill this work?"
print "A 16 bit unicode character: \u6C34"
print "Let's check this vertical tab: \v\vWhat is this?"
print "This should output an octal value: 1"
print "This should output a hex value: \xAA"
终端输出:
I'm tabbed in.
I'm split
on a line.
I'm \ a \ cat.
I'll do a list:
* Cat food
* Fishies
* Catnip
* Grass
This should ring a bell
Does this have a backspace
Printing "double quotes" and some 'single quotes'
Formfeed.
Will this work?
Linefeed.
Will this work?
Print the character named name: \N{name}
Will this work?e return.
A 16 bit unicode character: \u6C34
Let's check this vertical tab:
What is this?
This should output an octal value: I
This should output a hex value: �
不起作用的是:
- 铃铛:
\a
- 退格键:
\b
- 角色名称:
\N{name}
(已解决) - 16 位和 32 位 unicode 字符:
\u6C34
(已解决)
我应该怎么做才能使这些转义序列起作用?
================编辑=====================
print u"Print the character named name: \N{BLACK SPADE SUIT}"
print u"A 16 bit unicode character: \u6C34"
Print the character named name: ♠
A 16 bit unicode character: 水
- 16 位和 32 位 unicode 字符:
\u6C34
- 角色名:
\N{name}
当在字符串前面加上u
时,这些工作得很好
\a
和 \b
序列工作得很好,但您的 终端 可能会随意忽略它们。这些序列只是创建十六进制值分别为 07 和 08 的字节的别名:
>>> '\a'
'\x07'
>>> '\b'
'\x08'
两个序列各产生一个字节。由接收终端将字节解释为控制字符,终端可以自由地忽略该字节或对其执行不同的操作。
一些终端会闪烁而不是响铃;这通常是可配置的。在我的终端上 \b
可以很好地移动光标;它不会删除前面的字符。当您在其后添加更多文本时,位置的变化就会变得可见:
>>> print 'abc\b'
abc
>>> print 'abc\bd'
abd
d
覆盖了 c
字符,因为退格字符将光标位置移回了一个位置。
Unicode 转义序列仅适用于 Unicode 字符串,使用 u'...'
文字:
>>> print "A 16 bit unicode character: \u6C34"
A 16 bit unicode character: \u6C34
>>> print u"A 16 bit unicode character: \u6C34"
A 16 bit unicode character: 水
>>> print u"Print the character named: \N{CJK UNIFIED IDEOGRAPH-6C34}"
Print the character named: 水
请注意,它还取决于您的终端(控制台)是否真的支持打印的 Unicode 代码点!