Python文本文件格式

时间:2013-07-31 13:54:52

标签: python

这是文本文件

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和总行数之间。想法?

4 个答案:

答案 0 :(得分:3)

我不确定你想要什么,但我猜是这样的:

lines = []
buffer = []
for line in f:
    if 'date with' in line:
        lines.extend(["%s|%s" % (x, line) for x in buffer])            
        buffer = []
    else: 
        buffer.append(line)

# print lines
for line in lines:
    print line

# or save in a file
with open('myfile', 'w'):
    for line in lines:
         f.write(line)

答案 1 :(得分:1)

我的解决方案需要一个包含不以日期开头的内容的列表。

f = open('apple.txt')
lines = f.readlines()
f.close()
things = []
printtofile = []
for i in lines:
    things.append(i)
    if i.startswith('date'):
        things.pop()
        for x in things:
            if i[:-1] == '\n':
                printtofile.append(x[:-1]+'|'+i[:-1])
            else: 
                printtofile.append(x[:-1]+'|'+i)
        things = []
print printtofile
writefile = open('file.txt', 'w')
writefile.writelines(printtofile)
writefile.close()

希望它有所帮助,Python 2.7

答案 2 :(得分:0)

这是一个itertools方法:

from itertools import groupby, izip, product

with open('input') as fin, open('output', 'w') as fout:
    grouped = groupby(fin, lambda L: not L.startswith('date'))
    lines = ([el.strip() for el in g] for k, g in grouped)
    paired = izip(*[lines]*2)
    for pair in paired:
        fout.writelines('|'.join(items) + '\n' for items in product(*pair))

答案 3 :(得分:0)

我有两个解决方案,

从末尾读取文件,并按照流程收集项目,用简单的逻辑:

with open("file.txt") as f:
    lines = f.readlines()
output=[]
for line in reversed(lines):
        if line.startswith("date with"): msg = line
        else: output.append("{0}|{1}".format(line[:-1], msg))
for line in reversed(output): print line

以传统方式从文件顶部逐行:

granary = []
basket = []
with open("file.txt") as f:
    for line in f:
        basket.append(line)
        if line.startswith("date with"):
            granary += map(lambda x: "{0}|{1}".format(x[:-1], line), basket[:-1])
            del basket[:]
for item in granary: print item