问题:如何在Python中编写“标签”?

假设我有一个文件。如何写“你好” TAB“ alex”?

Let’s say I have a file. How do I write “hello” TAB “alex”?


回答 0

这是代码:

f = open(filename, 'w')
f.write("hello\talex")

\t字符串的内部是水平制表符的转义序列。

This is the code:

f = open(filename, 'w')
f.write("hello\talex")

The \t inside the string is the escape sequence for the horizontal tabulation.


回答 1

Python 参考手册包括几个可以在字符串中使用的字符串文字。这些特殊的字符序列被转义序列的预期含义代替。

这是一些更有用的转义序列的表格,并描述了它们的输出。

Escape Sequence       Meaning
\t                    Tab
\\                    Inserts a back slash (\)
\'                    Inserts a single quote (')
\"                    Inserts a double quote (")
\n                    Inserts a ASCII Linefeed (a new line)

基本范例

如果我想打印一些由制表符分隔的数据点,则可以打印此字符串。

DataString = "0\t12\t24"
print (DataString)

退货

0    12    24

清单范例

这是另一个示例,其中我们正在打印列表项,并且希望通过TAB来分隔项目。

DataPoints = [0,12,24]
print (str(DataPoints[0]) + "\t" + str(DataPoints[1]) + "\t" + str(DataPoints[2]))

退货

0    12    24

原始字符串

请注意,原始字符串(包含前缀“ r”的字符串),字符串文字将被忽略。这允许将这些特殊字符序列包含在字符串中而无需更改。

DataString = r"0\t12\t24"
print (DataString)

退货

0\t12\t24

这可能是不希望的输出

弦长

还应注意,字符串文字长度仅为一个字符。

DataString = "0\t12\t24"
print (len(DataString))

退货

7

原始字符串的长度为9。

The Python reference manual includes several string literals that can be used in a string. These special sequences of characters are replaced by the intended meaning of the escape sequence.

Here is a table of some of the more useful escape sequences and a description of the output from them.

Escape Sequence       Meaning
\t                    Tab
\\                    Inserts a back slash (\)
\'                    Inserts a single quote (')
\"                    Inserts a double quote (")
\n                    Inserts a ASCII Linefeed (a new line)

Basic Example

If i wanted to print some data points separated by a tab space I could print this string.

DataString = "0\t12\t24"
print (DataString)

Returns

0    12    24

Example for Lists

Here is another example where we are printing the items of list and we want to sperate the items by a TAB.

DataPoints = [0,12,24]
print (str(DataPoints[0]) + "\t" + str(DataPoints[1]) + "\t" + str(DataPoints[2]))

Returns

0    12    24

Raw Strings

Note that raw strings (a string which include a prefix “r”), string literals will be ignored. This allows these special sequences of characters to be included in strings without being changed.

DataString = r"0\t12\t24"
print (DataString)

Returns

0\t12\t24

Which maybe an undesired output

String Lengths

It should also be noted that string literals are only one character in length.

DataString = "0\t12\t24"
print (len(DataString))

Returns

7

The raw string has a length of 9.


回答 2

您可以在字符串文字中使用\ t:

"hello\talex"

You can use \t in a string literal:

"hello\talex"


回答 3

它通常\t在命令行界面中,它将把char \t转换为空白制表符。

例如,hello\talex-> hello--->alex

It’s usually \t in command-line interfaces, which will convert the char \t into the whitespace tab character.

For example, hello\talex -> hello--->alex.


回答 4

正如未在任何答案中提到的那样,以防万一您想要对齐和间隔文本时,可以使用字符串格式功能。(在python 2.5之上)当然\t是一个TAB令牌,而所描述的方法会生成空格。

例:

print "{0:30} {1}".format("hi", "yes")
> hi                             yes

另一个示例,左对齐:

print("{0:<10} {1:<10} {2:<10}".format(1.0, 2.2, 4.4))
>1.0        2.2        4.4 

As it wasn’t mentioned in any answers, just in case you want to align and space your text, you can use the string format features. (above python 2.5) Of course \t is actually a TAB token whereas the described method generates spaces.

Example:

print "{0:30} {1}".format("hi", "yes")
> hi                             yes

Another Example, left aligned:

print("{0:<10} {1:<10} {2:<10}".format(1.0, 2.2, 4.4))
>1.0        2.2        4.4 

回答 5

以下是一些获取“ hello” TAB“ alex”(使用Python 3.6.10测试)的更奇特的Python 3方法:

"hello\N{TAB}alex"

"hello\N{tab}alex"

"hello\N{TaB}alex"

"hello\N{HT}alex"

"hello\N{CHARACTER TABULATION}alex"

"hello\N{HORIZONTAL TABULATION}alex"

"hello\x09alex"

"hello\u0009alex"

"hello\U00000009alex"

实际上,代替使用转义序列,可以将制表符直接插入字符串文字中。这是带有制表符的代码,可用于复制和尝试:

"hello alex"

如果在复制字符串期间在上方字符串中的选项卡不会丢失,则“ print(repr(<上方字符串>)”应打印’hello \ talex’。

请参阅相应的Python文档以获取参考。

Here are some more exotic Python 3 ways to get “hello” TAB “alex” (tested with Python 3.6.10):

"hello\N{TAB}alex"

"hello\N{tab}alex"

"hello\N{TaB}alex"

"hello\N{HT}alex"

"hello\N{CHARACTER TABULATION}alex"

"hello\N{HORIZONTAL TABULATION}alex"

"hello\x09alex"

"hello\u0009alex"

"hello\U00000009alex"

Actually, instead of using an escape sequence, it is possible to insert tab symbol directly into the string literal. Here is the code with a tabulation character to copy and try:

"hello alex"

If the tab in the string above won’t be lost anywhere during copying the string then “print(repr(< string from above >)” should print ‘hello\talex’.

See respective Python documentation for reference.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。