Python文本文件格式

Python文本文件格式,python,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|d

这是文本文件

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.starts,并带有“date”,然后在到达该循环之前追加每一行,然后当x在0和总行数之间时,计数器重新开始。想法?

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

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)

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

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)

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

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()

希望有帮助,Python2.7

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

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提供了一种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))

以下是一种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))

我给你两个解决方案

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

打开(“file.txt”)作为f:
行=f.读行()
输出=[]
对于反向输入的行(行):
如果line.startswith(“date with”):msg=line
else:output.append(“{0}{1}.”格式(第[:-1]行,msg))
对于反向输入的行(输出):打印行
以传统方式,从文件的顶部逐行:

granary=[]
篮子=[]
将open(“file.txt”)作为f:
对于f中的行:
篮子。追加(行)
如果行开始日期(“日期日期”):
粮仓+=地图(lambda x:{0}{1})。格式(x[:-1],行),篮子[:-1])
del basket[:]
对于粮仓中的项目:打印项目

我为您提供了两种解决方案

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

打开(“file.txt”)作为f:
行=f.读行()
输出=[]
对于反向输入的行(行):
如果line.startswith(“date with”):msg=line
else:output.append(“{0}{1}.”格式(第[:-1]行,msg))
对于反向输入的行(输出):打印行
以传统方式,从文件的顶部逐行:

granary=[]
篮子=[]
将open(“file.txt”)作为f:
对于f中的行:
篮子。追加(行)
如果行开始日期(“日期日期”):
粮仓+=地图(lambda x:{0}{1})。格式(x[:-1],行),篮子[:-1])
del basket[:]
对于粮仓中的项目:打印项目


如何打印输出行=[]缓冲区=[]并将open('C:/Users/dit3442/Desktop/smallfruit.txt')作为infp:for line in infp:if'date with'in line:line.extend([%s |%s]%(x,line)for x in buffer])缓冲区=[]其他:buffer追加(line)我已经更新了响应,请检查;)我没有得到任何输出行=[]缓冲区=[]以open('C:/Users/dit3442/Desktop/smallfruit.txt')作为infp:for line in infp:if'date with'in line:lines.extend([%s |%s]%(x,line)for x in buffer])缓冲区=[]其他:缓冲区。追加(line)for line in line in line:print lines你确定吗?我得到了你的文件的预期结果。您是否正确读取了初始文件?检查它的内容(
f
在我这里的脚本中)我如何将输出行=[]缓冲区=[]以open('C:/Users/dit3442/Desktop/smallfruit.txt')打印为infp:for line in infp:if'date with'in line:line.extend([%s |%s]%(x,line)for x in buffer])buffer=[]其他:buffer.append(line)我已更新我的回复,请检查;)我没有得到任何输出行=[]缓冲区=[]以open('C:/Users/dit3442/Desktop/smallfruit.txt')作为infp:for line in infp:if'date with'in line:lines.extend([%s |%s]%(x,line)for x in buffer])缓冲区=[]其他:缓冲区。追加(line)for line in line in line:print lines你确定吗?我得到了你的文件的预期结果。您是否正确读取了初始文件?在我的脚本中检查它的内容(<代码> f>代码>)我如何编辑它来删除空白行?在打印语句中,只改变i(i:1)的变量i与x中的相同。我只是做了改变。由于某些原因,与狗的日期,它打印日期与DO。这是因为在最后一行没有返回。如果你给输入,那么最后一个CARCATER将是返回,而不是G的狗。我怎么可以编辑这个删除空白行?在打印语句只是改变变量i为I [(1)],我在X。我只是改变了。它用do?打印日期,这是因为最后一行没有返回。如果您输入enter,那么最后一个字符将是return,而不是dog的g。您需要什么帮助,或者您只是请求他人为您编写代码?这个问题是否有一个你不了解的特定方面?你需要什么帮助,或者你只是要求别人帮你写代码?这个问题是否有一个你不了解的具体方面?