从文本文件中的不同行读取特定数字,并将其添加到文本中每个行块的末尾

时间:2013-06-25 20:15:58

标签: python list text add

我是python的新手,已经解决了我搜索和阅读本网站的许多问题。但是现在是时候问我......


我有一个带有以下结构的txt文件:

SETUP

    STN_NO  419430403
    STN_ID  "S1"
    INST_HT 1.545000;
END SETUP
SLOPE (TgtNo, TgtID, CfgNo, Hz, Vz, SDist, RefHt, Date, Ppm, ApplType, Flags)
    419430405,  "S2",   1,  0.000000,   98.799682,  12.056200,  1.700000,   18-10-2012/10:06:08.0,  0.000000,   107,    00000000;
    419430407,  "1",    1,  0.000052,   98.799806,  12.056800,  1.700000,   18-10-2012/10:06:16.0,  0.000000,   107,    00000000;
    419430409,  "2",    2,  78.734236,  99.822405,  17.919000,  0.000000,   18-10-2012/10:09:50.0,  0.000000,   107,    00000000;
    419430410,  "3",    2,  78.861726,  108.352791, 17.213700,  0.000000,   18-10-2012/10:10:10.0,  0.000000,   107,    00000000;
END SLOPE

SETUP
    STN_NO  419430459
    STN_ID  "1"
    INST_HT 1.335000;
END SETUP
SLOPE (TgtNo, TgtID, CfgNo, Hz, Vz, SDist, RefHt, Date, Ppm, ApplType, Flags)
    419430462,  "S1",   5,  122.545107, 99.563594,  12.056300,  1.700000,   18-10-2012/11:04:36.0,  0.000000,   107,    00000000;
    419430464,  "50",   5,  200.000125, 99.563463,  12.058800,  1.700000,   18-10-2012/11:04:44.0,  0.000000,   107,    00000000;
    419430466,  "51",   6,  60.723043,  95.842462,  8.607300,   0.000000,   18-10-2012/11:06:36.0,  0.000000,   107,    00000000;
    419430467,  "52",   6,  99.683958,  95.664912,  7.581100,   0.000000,   18-10-2012/11:08:15.0,  0.000000,   107,    00000000;
    419430468,  "53",   6,  101.389131, 87.173327,  7.853000,   0.000000,   18-10-2012/11:08:51.0,  0.000000,   107,    00000000;
END SLOPE
END THEODOLITE

问题是我想在每行的末尾添加正确的INST_HT值(意味着SLOPE和END SLOPE之间的第一个数据块中的1.545000和第二个中的1.335000等)。

目标是创建一个正确的csv文件,其中包含TgtID,Hz,Vz,SDist,RefHt列(已经完成)和INST_HT(错过那个!!!)的数字数据。

到目前为止,唯一的想法就是创建一个列表,其中包含从打开到文件末尾的所有INST_HT值。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

这应该适用于您描述的问题:

INST_HT = [1.545000,
           1.335000]
lines = open('tmp.txt')
out = open('tmp2.txt', 'w')
i = -1
while True:
    try:
        line = lines.next()
    except StopIteration:
        break
    if 'slope' in line.lower():
        i += 1
        out.write(line)
        while True:
            line = lines.next()
            if 'end slope' in line.lower():
                out.write(line)
                break
            else:
                out.write('    ' + line.strip()[:-1] + ', ' + str(INST_HT[i]) + ';\n')
    else:
        out.write(line)
out.close()

答案 1 :(得分:0)

以这种方式思考问题:你想逐行,并对每一行做不同的事情。

last_inst_ht = None
in_slope = False
with open('infile.txt') as infile, open('outfile.txt') as outfile:
    for line in infile:
        if line.startswith('SLOPE'):
            bits = line.split(')')
            line = bits[0] + ', INST_HT' + bits[1]
            in_slope = True
        elif line.startswith('END SLOPE'):
            in_slope = False
        elif in_slope:
            bits = line.split(';')
            line = bits[0] + ', ' + last_inst_ht + bits[1]
        elif line.strip().startwith('INST_HT'):
            last_inst_ht = line.strip().split()[-1][:-1]
        outfile.write(line)

通过跟踪更多状态信息,您可以使其更加健壮。如果您在INST_HT之外得到SETUP,也许您应该打印警告或错误。或者,如果在SETUP之前获得两个SLOPE块,或者没有。或者,如果您获得SETUP而没有INST_HT。等等。

另外,我解析这些行的方式并不完全健壮。例如,如果您的某个字段中有;,我们会将last_inst_ht改为该字段的中间而不是结尾。但是我希望保持简单和简洁,希望你能理解逻辑,而不是盲目地复制它,所以你可以在将来自己扩展和调试它。