Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “如何分割”\";在字符串中添加两个单独的字符,如\&引用;及;t";?(如何分割转义序列?)_Python_String_Python 3.x - Fatal编程技术网

Python “如何分割”\";在字符串中添加两个单独的字符,如\&引用;及;t";?(如何分割转义序列?)

Python “如何分割”\";在字符串中添加两个单独的字符,如\&引用;及;t";?(如何分割转义序列?),python,string,python-3.x,Python,String,Python 3.x,我正在尝试将python中的字符串拆分为字符列表。我知道在python中有很多方法可以做到这一点,但我有一个例子,这些方法不能给我期望的结果 当我在字符串中显式地写入了诸如“\t”之类的特殊字符(我不是指真正的制表符)时,就会出现问题 例如: string = " Hello \t World." > list(string) > ['H', 'e', 'l', 'l', 'o', 'w', ' ', '\t', ' ', 'W', 'o', 'r', 'l', 'd', '

我正在尝试将python中的字符串拆分为字符列表。我知道在python中有很多方法可以做到这一点,但我有一个例子,这些方法不能给我期望的结果

当我在字符串中显式地写入了诸如“\t”之类的特殊字符(我不是指真正的制表符)时,就会出现问题

例如:

string = "    Hello \t World."
> list(string)
> ['H', 'e', 'l', 'l', 'o', 'w', ' ', '\t', ' ', 'W', 'o', 'r', 'l', 'd', '.']
我需要的输出是:

list_of_chars = [' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '\', 't', ' ', 'W', 'o', 'r', 'l', 'd', '.']
但是当我使用中给出的方法时,我会得到一个列表,其中包含“/t”作为整个字符串,而不是分开的

例如:

string = "    Hello \t World."
> list(string)
> ['H', 'e', 'l', 'l', 'o', 'w', ' ', '\t', ' ', 'W', 'o', 'r', 'l', 'd', '.']

我想知道为什么会发生这种情况,以及如何得到我想要的。

\t
表示制表符,如果您想明确使用
\
字符,则需要在字符串中转义它:

string = "    Hello \\t World."
或者使用原始字符串:

string = r"    Hello \t World."
您应该看一看文件,上面写着:

反斜杠(
\
)字符用于转义具有特殊含义的字符,例如换行符、反斜杠本身或引号字符。字符串文本可以选择前缀字母
r'或
r';这些字符串称为原始字符串,并对反斜杠转义序列使用不同的规则

在示例字符串中,
\t
不是两个字符,而是表示ASCII水平制表符(制表符)的单个字符

为了告诉Python解释器这两个字符是独立的,您应该使用原始字符串(在字符串之前使用r”),如下所示:

但在这里,您还将在结果列表中看到两个
\
,这只是Python表示
\
的方式

对于Python解释器,
'\'
是无效字符串,因为字符串中的
\'
表示单引号(')。因此,当您执行
'\'
时,会引发以下错误,因为对于Python,字符串中没有结束引号:

>>> '\'
  File "<stdin>", line 1
    '\'
      ^
SyntaxError: EOL while scanning string literal
因为它是一个字节字符串,所以需要调用以获取每个字节对应的字符值。例如:

>>> list(map(chr, unicode_escaped_string))
[' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '\\', 't', ' ', 'W', 'o', 'r', 'l', 'd', '.']

您可以相应地替换字符串:

import itertools
txt = "    Hello \t World."

specials = { 
    '\a' : '\\a', #     ASCII Bell (BEL)
    '\b' : '\\b', #     ASCII Backspace (BS)
    '\f' : '\\f', #     ASCII Formfeed (FF)
    '\n' : '\\n', #     ASCII Linefeed (LF)
    '\r' : '\\r', #     ASCII Carriage Return (CR)
    '\t' : '\\t', #     ASCII Horizontal Tab (TAB)
    '\v' : '\\v'  #     ASCII Vertical Tab (VT)
}

# edited out: # txt2 = "".join([x if x not in specials else specials[x] for x in txt])
txt2 = itertools.chain(* [(list(specials[x]) if x in specials else [x]) for x in txt])

print(list(txt2))
输出:

[' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '\\', 't', ' ', 'W', 
 'o', 'r', 'l', 'd', '.'] 

列表理解看起来更“积极”,并使用
list(itertools.chain(*[…])
而不是
list(“”.join([…])
,后者应该更有效。

您可以转换成Python的文本字符串,然后逐个字符拆分

string = "    Hello \t World."
string_raw = string.encode('unicode-escape')
print([ch for ch in string_raw])
print([chr(ch) for ch in string_raw])
产出:

[32, 32, 32, 32, 72, 101, 108, 108, 111, 32, 92, 116, 32, 87, 111, 114, 108, 100, 46]
[' ', ' ', ' ', ' ', 'H', 'e', 'l', 'l', 'o', ' ', '\\', 't', ' ', 'W', 'o', 'r', 'l', 'd', '.']

Ascii
92
是一个单独的反冲,即使当您在终端中打印它时,它也会显示它已转义。

string=[x代表x在r“Hello\t World.”]
是最接近的。您需要的输出是不可能的。这是一个语法错误。如果在Python中键入
“Hello\t World.”
,这是真正的选项卡。包含反斜杠-t的字符串可以是
r“Hello\t World.”
Hello\\t World.
。您在代码中有字符串还是正在读取文件?..@dividebyzero我正在读取一个类似高级语言的源代码文件,它有我提到的字符串。@vaultah Cool。只有关于
[key]
.keys
-a
.get()
的新的默认值-应该认为会提供它。在此处找到:。我读过有关语法的书,但头脑不清楚。谢谢你的评论-下一步我将阅读翻译。