Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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是否将^split()中的空格视为空白?_Python_Python 3.x_Replace_Split - Fatal编程技术网

Python是否将^split()中的空格视为空白?

Python是否将^split()中的空格视为空白?,python,python-3.x,replace,split,Python,Python 3.x,Replace,Split,我有一个错误代码列表,其中一个代码是: Dial code generated note %d on stn ^char, cct %d^cct_chk^cct_chk 任何包含%、^或$符号的术语都是变量,我需要用替换整个变量术语 所以上面的一行应该是这样的: Dial code generated note <*> on stn <*> cct <*> stn cct上生成的拨号代码注释 为此,我写了: message = "Dial code ge

我有一个错误代码列表,其中一个代码是:

Dial code generated note %d on stn ^char, cct %d^cct_chk^cct_chk
任何包含
%
^
$
符号的术语都是变量,我需要用
替换整个变量术语

所以上面的一行应该是这样的:

Dial code generated note <*> on stn <*> cct <*>
stn cct上生成的拨号代码注释 为此,我写了:

message = "Dial code generated note %d on stn ^char, cct %d^cct_chk^cct_chk"
for term in message.split():
    if '^' in term:
        message = message.replace(term, '<*>')
    if '$' in term:
        message = message.replace(term, '<*>')
    if '%' in term:
        message = message.replace(term, '<*>')
print (message)
message=“拨号代码在stn^char、cct%d^cct\u chk^cct\u chk上生成了注释%d”
对于message.split()中的术语:
如果术语为“^”:
message=message.replace(术语“”)
如果术语为“$”:
message=message.replace(术语“”)
如果术语为“%”:
message=message.replace(术语“”)
打印(信息)
对于大多数消息来说,这似乎是有效的。但对于这个例子,我得到:

>>Dial code generated note <*> on stn <*> <*> <*>^<*>_chk^<*>_chk
>stn上生成的拨号代码注释
它似乎将
^
视为一个空格,否则最后一个术语应该全部替换为

有人能告诉我这是为什么吗?

你可以把你喜欢的部分重新拆分和组装起来:

message = "Dial code generated note %d on stn ^char, cct %d^cct_chk^cct_chk"

message = ' '.join('<*>' if any(item in term for item in '^$%')
                    else term for term in message.split())
print(message)
message=“拨号代码在stn^char、cct%d^cct\u chk^cct\u chk上生成了注释%d”
message=''.join(''如果有('^$%'中项目的术语中的项目)
else术语,用于message.split()中的术语
打印(信息)
输出:

Dial code generated note <*> on stn <*> cct <*>
stn cct上生成的拨号代码注释
您可以拆分并重新组装您喜欢的部件:

message = "Dial code generated note %d on stn ^char, cct %d^cct_chk^cct_chk"

message = ' '.join('<*>' if any(item in term for item in '^$%')
                    else term for term in message.split())
print(message)
message=“拨号代码在stn^char、cct%d^cct\u chk^cct\u chk上生成了注释%d”
message=''.join(''如果有('^$%'中项目的术语中的项目)
else术语,用于message.split()中的术语
打印(信息)
输出:

Dial code generated note <*> on stn <*> cct <*>
stn cct上生成的拨号代码注释
replace
有第三个参数,用于限制替换次数。在您的情况下,问题是由多次使用替换字符串引起的:
%d
在最后一个单词中被替换

一个简单的解决方法是将替换数量限制为1:

message = "Dial code generated note %d on stn ^char, cct %d^cct_chk^cct_chk"
for term in message.split():
    if '^' in term:
        message = message.replace(term, '<*>', 1)
    if '$' in term:
        message = message.replace(term, '<*>', 1)
    if '%' in term:
        message = message.replace(term, '<*>', 1)
print (message)
message=“拨号代码在stn^char、cct%d^cct\u chk^cct\u chk上生成了注释%d”
对于message.split()中的术语:
如果术语为“^”:
message=message.replace(术语“”,1)
如果术语为“$”:
message=message.replace(术语“”,1)
如果术语为“%”:
message=message.replace(术语“”,1)
打印(信息)

顺便说一句,只要在每次迭代中打印
术语
消息
,错误就会很明显。

replace
有第三个参数,允许限制替换次数。在您的情况下,问题是由多次使用替换字符串引起的:
%d
在最后一个单词中被替换

一个简单的解决方法是将替换数量限制为1:

message = "Dial code generated note %d on stn ^char, cct %d^cct_chk^cct_chk"
for term in message.split():
    if '^' in term:
        message = message.replace(term, '<*>', 1)
    if '$' in term:
        message = message.replace(term, '<*>', 1)
    if '%' in term:
        message = message.replace(term, '<*>', 1)
print (message)
message=“拨号代码在stn^char、cct%d^cct\u chk^cct\u chk上生成了注释%d”
对于message.split()中的术语:
如果术语为“^”:
message=message.replace(术语“”,1)
如果术语为“$”:
message=message.replace(术语“”,1)
如果术语为“%”:
message=message.replace(术语“”,1)
打印(信息)

顺便说一句,只要在每次迭代中打印
术语
消息
,错误就会很明显……

您已经将所有
%d
替换为
,因此当您到达最后一个术语时,它不再是拆分版本中的内容。简短回答:不,不是。但是您的输出与您发布的代码不太匹配。啊,好的。你知道在这种情况下我会如何修复它,以便它提供我想要的输出吗?使用你的示例代码,我在stn cct^cct\U chk^cct\U chk
输出上得到了
拨号代码生成的注释,它与你的示例输出不同。是的,它可能是。我经历了一些不同的组合,可能是复制了错误的组合。重要的部分是所需的输出您已经将所有的
%d
替换为
,因此当您到达最后一个术语时,它不再是拆分版本中的内容。简短回答:不,不是。但是您的输出与您发布的代码不太匹配。啊,好的。你知道在这种情况下我会如何修复它,以便它提供我想要的输出吗?使用你的示例代码,我在stn cct^cct\U chk^cct\U chk
输出上得到了
拨号代码生成的注释,它与你的示例输出不同。是的,它可能是。我经历了一些不同的组合,可能是复制了错误的组合。重要的部分是期望的输出