使用python重新排列文本文件中的数据时出现问题

时间:2015-01-30 22:29:42

标签: python

我正在研究一些可以做很多奇怪事情的代码。其中一个奇怪的事情是它生成一个名为combined.glm的文件,该文件的内容如下所示:

object line_space {
    content;
    content;
}

object line_conf {
    content;
    content;
}
#2 more line_conf objects

object line_ug { 
    content;
    content;
}
#70 more line_ug

object nod_l {
    content;
    content;
}
#65 more nod_l

我想移动nod_l部分之上的所有line_ug部分,即line_conf部分之后的部分。我想做的方式是这样的:

...
noding=open("combined.glm",'r').readlines()
combined_order=open("combined_order.glm",'w')
for ko,comblineo in enumerate(noding):
    if 'object line_space {' in comblineo:
        combined_order.writelines(noding[ko:ko+5])
for kt,comblinet in enumerate(noding):
    if 'object line_conf {' in comblinet:
        combined_order.writelines(noding[kt:kt+5])
for kr,combliner in enumerate(noding):
    if 'object nod_l {' in combliner:
        combined_order.writelines(noding[kr:kr+5])
for kf,comblinef in enumerate(noding):
    if 'object line_ug' in comblinef:
        combined_order.writelines(noding[kf:kf+5])
...

但它不起作用(在我看来,这是有道理的)。我有大约28 nod_l正确复制,然后很多null个字符和大约16 line_ug然后另一个nod_l并且中断。我不确定发生了什么。

1 个答案:

答案 0 :(得分:2)

这很脏,但确实有效。

import re

old = open("combined.glm").read()

space = re.compile("object line_space {.*?}",re.DOTALL + re.MULTILINE)
conf = re.compile("object line_conf {.*?}",re.DOTALL + re.MULTILINE)
ug = re.compile("object line_ug {.*?}",re.DOTALL + re.MULTILINE)
nod = re.compile("object nod_l {.*?}",re.DOTALL + re.MULTILINE)

all_space = space.findall(old)
all_conf = conf.findall(old)
all_ug = ug.findall(old)
all_nod = nod.findall(old)

with open("combined_order.glm","w") as f:
    for thing in all_space:
        f.write(thing + "\n\n")
    for thing in all_conf:
        f.write(thing + "\n\n")
    for thing in all_nod:
        f.write(thing + "\n\n")
    for thing in all_ug:
        f.write(thing + "\n\n")