python - 使用python在一行中的一组字符之前添加一个新行

标签 python string

我有一行巨大的字符,其中一组字符不断重复。该行是: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/

相关文章:

python - 执行 re.search() 直到行尾,然后更新数据帧

Python - 保存除大属性之外的整个类对象

python - 在虚拟工作室上运行时出现 "Failed to find a kernelspec to use for ipykernel launch"错误

c++ - 尝试在 getline 中使用 int

java - 如何将\n 与打印的字符串文字结合使用?

python - 通过 rpy2 使用 Rblpapi

从 JSON 模式生成 Python JSON 虚拟数据

ios - Objective-C 如何调用outlet?

c# - Debug.WriteLine ("Put text here: {0}"myString 的输出不正确)

linux - 将两个或多个空格更改为单个分号