我有一行巨大的字符,其中一组字符不断重复。该行是:qwethisistheimportantpartqwethisisthesecondimportantpart
字符串中没有空格。我想在字符串“qwe”之前添加一个新行,以便我可以区分每个重要部分。
输出:
qwethisistheimportantpart
qwethisisthesecondimportantpart
我试过用
for line in infile:
if line.startswith("qwe"):
line="\n" + line
好像没用
最佳答案
str.replace()
可以做你想做的事:
line = 'qwethisistheimportantpartqwethisisthesecondimportantpart'
line = line.replace('qwe', '\nqwe')
print(line)
关于python - 使用python在一行中的一组字符之前添加一个新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45039268/