【问题标题】:read specific number from different lines in a txt file and add it to the end of each line block in txt从 txt 文件中的不同行读取特定数字并将其添加到 txt 中每个行块的末尾
【发布时间】:2013-06-25 20:15:58
【问题描述】:

我对 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 值。

有什么想法吗?

【问题讨论】:

  • 你能展示你现有的代码吗?

标签: python list text add


【解决方案1】:

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

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

【讨论】:

  • 为什么要硬编码 INST_HT 值而不是使用文件中的值?
【解决方案2】:

这样想问题:你想一行一行地去,每行做不同的事情。

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)

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

另外,我解析行的方式并不完全可靠。例如,如果您的某个字段中有 ;,我们会将 last_inst_ht 放在该字段的中间而不是末尾。但我想让事情简单明了,希望你能理解其中的逻辑,而不是盲目地复制它,这样你以后可以自己扩展和调试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多