Python将文本文件的格式更改为新格式

时间:2012-06-01 23:04:43

标签: python parsing lines

我提供的文本文件格式如下:

3     Bham    Hoover - Vestiva 
123 234 1 456 876 1 876 745 1
0
4     Bham    Vestiva - Greensprings
235 876 1 647 987 1 098 765 1 234 546 1
0

这还有几行,但我正在尝试将此格式转换为以下内容:

Event
Disconnect branch from bus 123 to 234 circuit 1
Disconnect branch from bus 456 to 876 circuit 1
Disconnect branch from bus 876 to 745 circuit 1
end
Event 
Disconnect branch from bus 235 to 876 circuit 1
Disconnect branch from bus 647 to 987 circuit 1
Disconnect branch from bus 098 to 765 circuit 1
Disconnect branch from bus 234 to 546 circuit 1
end

3 个答案:

答案 0 :(得分:1)

from itertools import islice
with open('file.txt', 'r') as f:
    # iterate over every 3rd line, starting with the 2nd
    for line in islice(f, 1, None, 3):
        parts = line.split()
        print 'Event'
        # iterate over 3-element chunks
        for x in zip(*(iter(parts),) * 3):
            print 'Disconnect branch from bus %s to %s circuit %s' % x
        print 'end'

输出:

Event
Disconnect branch from bus 123 to 234 circuit 1
Disconnect branch from bus 456 to 876 circuit 1
Disconnect branch from bus 876 to 745 circuit 1
end
Event
Disconnect branch from bus 235 to 876 circuit 1
Disconnect branch from bus 647 to 987 circuit 1
Disconnect branch from bus 098 to 765 circuit 1
Disconnect branch from bus 234 to 546 circuit 1
end

对分块代码的信用转到Iteration over list slices

答案 1 :(得分:0)

with open("data.txt") as inf:
    for head,data,tail in zip(inf,inf,inf):
        print("Event")
        items = iter(data.split())
        for bus1,bus2,cct in zip(items,items,items):
            print("Disconnect branch from bus {} to {} circuit {}".format(bus1,bus2,cct))
        print("end")

结果

Event
Disconnect branch from bus 123 to 234 circuit 1
Disconnect branch from bus 456 to 876 circuit 1
Disconnect branch from bus 876 to 745 circuit 1
end
Event
Disconnect branch from bus 235 to 876 circuit 1
Disconnect branch from bus 647 to 987 circuit 1
Disconnect branch from bus 098 to 765 circuit 1
Disconnect branch from bus 234 to 546 circuit 1
end

答案 2 :(得分:-1)

我不确定你真正的需要。但是如果你想像你展示的那样输出你的文件,你可以在你的文本上插入一个分隔符,然后使用re模块进行解析,然后按照你需要的顺序获取数据!