这是文本文件
apple1
apple2
apple3
date with apple
flower1
flower2
flower3
flower4
date with flower
dog1
dog2
date with dog
我需要一个 python 代码来帮助我把文件变成这样的东西
apple1|date with apple
apple2|date with apple
apple3|date with apple
flower1|date with flower
flower2|date with flower
flower3|date with flower
flower4|date with flower
dog1|date with dog
dog2|date with dog
它可能需要一个嵌套循环,一个在 line.startswith "date" 之前计数,然后当它到达那里时,它会在它之前附加每一行,然后计数器重新开始,而 x 介于 0 和总行数之间。想法?