简体   繁体   English

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

[英]read specific number from different lines in a txt file and add it to the end of each line block in txt

I am realy new with python and already solved many of my problems searching and reading this site. 我对python真的很陌生,已经解决了我在搜索和阅读此站点时遇到的许多问题。 But now the time had come for me to ask... 但是现在是时候问我了...


I have a txt file with the below structure: 我有一个具有以下结构的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

The problem is that i want to add to the end of each line the proper INST_HT value (meaning 1.545000 in the first block of data between SLOPE and END SLOPE and 1.335000 in the second, etc). 问题是我想在每行的末尾添加适当的INST_HT值(意味着在SLOPE和END SLOPE之间的第一个数据块中为1.545000,在第二个数据块中为1.335000,依此类推)。

The goal is to create a proper csv file containing the numeric data of TgtID, Hz, Vz, SDist, RefHt columns (already done that) and INST_HT (missed that one!!!). 目标是创建一个适当的csv文件,其中包含TgtID,Hz,Vz,SDist,RefHt列(已经完成)和INST_HT(缺少该代码!)的数字数据。

Until now the only think that i have done is creating a list with all the INST_HT values from beggining to the end of the file. 直到现在,我唯一想到的就是创建一个列表,其中包含从开始到文件末尾的所有INST_HT值。

Any ideas? 有任何想法吗?

This should work for the problem you described: 这应该可以解决您描述的问题:

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

Think about the problem this way: You want to go line by line, and do different things with each line. 以这种方式思考问题:您想逐行进行,并对每行做不同的事情。

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)

You can make this a bit more robust by keeping track of more state information. 通过跟踪更多状态信息,可以使此过程更可靠。 If you get an INST_HT outside of a SETUP , maybe you should print a warning or an error. 如果您在SETUP之外获得INST_HT ,则可能应打印警告或错误。 Or if you get two SETUP blocks, or none, before a SLOPE . 或者,如果在SLOPE之前得到两个SETUP块,或者没有,则没有。 Or if you get a SETUP without an INST_HT in it. 或者,如果您获得的SETUP没有INST_HT And so on. 等等。

Also, the way I'm parsing the lines isn't exactly robust. 另外,我解析行的方式也不是很可靠。 For example, if you could have a ; 例如,如果您可以使用; in one of your fields, we'd instead the last_inst_ht into the middle of that field instead of at the end. 在您的一个字段中,我们last_inst_ht为该字段的中间而不是结尾。 But I wanted to keep things simple and brief in hopes that you'd understand the logic instead of just copying it blindly, so you can expand and debug it yourself in the future. 但是我想让事情简单明了,希望您能理解逻辑而不是盲目地复制它,以便将来自己扩展和调试它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM