【发布时间】:2018-08-02 18:13:57
【问题描述】:
参考上一个问题Python Regex - Capture match and previous two lines
我正在尝试将此匹配写入文本文件,但它似乎将所有匹配写入 1 行。
尝试了这些组合,但没有成功
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 并保持原始格式正确?
【问题讨论】:
-
myfilename.write("\n".join(output))或myfilename.write("\r\n".join(output)) -
啊,谢谢,我搜索了,但可能没有字
-
对*谢谢。
标签: python regex python-3.x