简体   繁体   English

Python 在多行字符串中替换

[英]Python replace within multiline string

One of my modules (t_mesg.py) has a multi-line string:我的一个模块 (t_mesg.py) 有一个多行字符串:

tm = """
this
is currently
a
test
message
"""

I import this in another module where I would need to replace certain parts of the string with others.我将它导入到另一个模块中,我需要将字符串的某些部分替换为其他部分。 However, on the import, the newline chars come in as well and so tm.replace(...) would not work.但是,在导入时,换行符也会出现,因此tm.replace(...)不起作用。

>>> from t_mesg import tm
>>> tm
'\nthis\nis \na\ntest\nmessage\n'

If I need to process this imported string to change "is" to "is not" how would I go about it, so that the string looks like this?如果我需要处理这个导入的字符串以将“是”更改为“不是”,我将如何处理,使字符串看起来像这样?

tm = """
this
is not currently
a
test
message
"""

TL;DR - how can I perform the replacement ignoring the newline chars? TL; DR - 如何在忽略换行符的情况下执行替换?

Basically you want to perform a word replacement in a string.基本上你想在字符串中执行单词替换。 You can do it using regular expressions & word boundaries, and the hell with newlines or not:您可以使用正则表达式和单词边界来做到这一点,以及是否使用换行符:

import re进口重新

s = "this\n is \n a good \n question"
s = re.sub(r"\bis\b","is not",s)
print(s)

result:结果:

this
 is not 
 a good 
 question

You can revert back with this (which allows some more newlines to be present between both words and preserves them)您可以使用此还原(这允许在两个单词之间出现更多换行符并保留它们)

s = re.sub(r"\bis\b(\s+)\bnot\b",r"is\1",s)
print(s)

prints:印刷:

this
 is 
 a good 
 question

to go a little further, you could introduce punctuation and other non-alpha stuff and using \\W you could still manage:更进一步,您可以引入标点符号和其他非 alpha 内容并使用\\W您仍然可以管理:

s = "this\n is - not - \n a good \n question"
s = re.sub(r"\bis(\W+)not\b",r"is\1",s)
print(s)

prints ("not" has gone away but not the dash before it):打印(“不”已经消失,但它之前的破折号没有):

this
 is -  - 
 a good 
 question

The replace method doesn't store the altered value in the same variable. replace 方法不会将更改后的值存储在同一变量中。 You need to store it in another variable and print it.您需要将其存储在另一个变量中并打印出来。

tm = tm.replace('\nis \n', '\nis not\n')

you could try splitting up the string, replacing the word and joining the array back together.您可以尝试拆分字符串,替换单词并将数组重新连接在一起。

tm_array = tm.split("\n")
tm_array[1] = "is not"
new_tm = '\n'.join(tm_array)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM