[英]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: \111"
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}
(已解決) \水
(已解決) 我應該怎么做才能使這些轉義序列起作用?
================編輯=====================
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: 水
\水
\\N{name}
當在字符串前面加一個u
時,它們可以正常工作 \\a
和\\b
序列可以正常工作,但是您的終端可以隨意忽略它們。 這些序列只是用於分別創建具有十六進制值07和08的字節的別名:
>>> '\a'
'\x07'
>>> '\b'
'\x08'
這兩個序列各產生一個字節。 由接收終端將字節解釋為控制字符,終端可以隨意忽略該字節或對其進行其他操作。
有些終端會閃爍而不是響鈴。 這通常是可配置的。 在我的終端上, \\b
可以很好地移動光標; 它不會刪除前面的字符。 當您在其后添加更多文本時,位置變化將變得可見:
>>> print 'abc\b'
abc
>>> print 'abc\bd'
abd
d
重寫了c
字符,因為退格字符將光標位置移回了一個位置。
Unicode轉義序列只能使用u'...'
文字在Unicode字符串中工作:
>>> 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代碼點,則還取決於您的終端(控制台)!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.