Python-使用re.findall中的\n将列表写入文本文件

Python-使用re.findall中的\n将列表写入文本文件,python,regex,python-3.x,Python,Regex,Python 3.x,关于上一个问题 我试图将此匹配写入文本文件,但它似乎将所有匹配写入一行 尝试了这些组合,但运气不佳 output = re.findall(r'(?:.*\r?\n){2}.*?random data.*', f.read()) myfilename.write(str(list(output) + '\n')) # gives me TypeError: can only concatenate list (not "str") to list myfilename.write(st

关于上一个问题

我试图将此匹配写入文本文件,但它似乎将所有匹配写入一行

尝试了这些组合,但运气不佳

    output = re.findall(r'(?:.*\r?\n){2}.*?random data.*', f.read())

myfilename.write(str(list(output) + '\n')) # gives me TypeError: can only concatenate list (not "str") to list
myfilename.write(str(output)) # writes to one line
我是否需要一个for循环来将每个索引迭代到新行,或者我是否遗漏了什么,它应该与CRLLF匹配并保持原始格式正确?

您可以使用

with open ("file_here.txt", "r") as fin, open("output.txt", "w") as fout:
    output = re.findall(r'(?:.*\r?\n){2}.*?random data.*', fin.read())
    fout.write("\n".join(output))

myfilename.write(“\n”.join(output))
myfilename.write(“\r\n”.join(output))
啊,谢谢,我搜索过了,但可能没有写对*谢谢。