0

这是文本文件

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

4 回答 4

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)
于 2013-07-31T13:58:13.353 回答
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

于 2013-07-31T14:11:00.530 回答
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))
于 2013-07-31T14:43:42.070 回答
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
于 2013-07-31T15:05:39.397 回答