按特定顺序替换行

时间:2018-12-11 10:05:49

标签: regex

我有一个记事本文件,包含超过5k的块,例如:

title
subtitle
date

title
subtitle
date

title
subtitle
date

自动获得输出的最佳解决方案是:

date
title
subtitle

date
title
subtitle

date
title
subtitle

1 个答案:

答案 0 :(得分:0)

如果您的计算机上装有python,请尝试运行以下代码

# edit these paths manually path = 'data.txt' newFilePath = 'data2.txt' with open(path, 'r') as oldFile: with open(newFilePath, 'w') as newFile: num = 0 stringTemp = [] for line in oldFile: if line == '\n': newFile.write("\n") else: stringTemp.append(line) num +=1 if num == 3: newFile.write(stringTemp[2]) newFile.write(stringTemp[0]) newFile.write(stringTemp[1]) num = 0 stringTemp.clear() assert len(stringTemp) == 0 newFile.close() oldFile.close()