Python,在解析字符串时,如果是字符,则换行

时间:2018-07-17 16:45:32

标签: python

如果我有如下所示的字符串(出于示例目的而简化)

myString = 

"Hello world, the weather is nice today} How are you today?"

有没有一种方法可以解析字符串,并且

if "}" then \n

所以我的最终结果将是

"Hello world, the weather is nice today}
How are you today?"

3 个答案:

答案 0 :(得分:2)

是的,使用 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Title</title> <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous"> <style media="screen" type="text/css"> img { width: 100%; } .btn { margin: auto; } .vertical-center { height:100%; width:100%; } .left-text { text-align: left; } </style> </head> <body class="verticalcenter"> <div class="container vertical-center"> <div class="row"> <div class="col-xs-12 col-sm-12 col-lg-6"> <img src="./profile.jpg"> </div> <div class="col-xs-12 col-sm-12 col-lg-5"> <div class="row left-text"> <div class="col-xs-12 col-md-8"> <h2>MyStackFlowethOver</h2> </div> <div class="col-xs-12 col-md-8"> <h4>web developer</h4> </div> <div class="col-xs-12 col-md-8"> <a href="#" class="" role="button" target="_blank">github</a> </div> <div class="col-xs-12 col-md-8"> <a href="#" class="" role="button" target="_blank">linkedin</a> </div> <div class="col-xs-12 col-md-8"> <a href="3" class="" role="button" target="_blank">twitter</a> </div> <div class="col-xs-12 col-md-8"> <a href="/blog" class="" role="button" target="_blank">blog</a> </div> <div class="col-xs-12 col-md-8"> <a href="./resume.pdf" class="" role="button" target="_blank">resume</a> </div> </div> </div> </div> </div> </body> </html> 方法:

myString.replace("}","}\n")

输出:

replace()

答案 1 :(得分:1)

您可以MERGE2=Doe

答案 2 :(得分:1)

许多人描述的最简单的方法是用replace } }\n

但是,如果您觉得自己很花哨,可以将look-behind与正则表达式一起使用;-)

>>> re.sub('(?<=})','\n',s)

#driver值:

IN : s = "Hello world, the weather is nice today} How are you today?"
OUT : 'Hello world, the weather is nice today}\n How are you today?'