Python:在匹配行之后从文件中提取 3 行

我有一个包含数据的文本文件。它包含比我需要的更多信息,因此我试图仅提取标题为“温度”的有关温度的部分。我需要用它提取三行数据,然后最终创建一个仅包含相关数据的新文本文件。


以下是文本文件“Test_File.txt”的示例:


NOT IMPORTANT

234123 1523 1234 613 1234 146134 51234 123231 123 1235123512 

5467 3 564 245 26 234 5 62 435 234 534 62 345 2346 234 52 345 2345 2

456 2345 2362 3452 346 2345 236 254 24 523 45 23462 345 234 54326 23

TEMPERATURE

11223 112312 4123123 6423 123124 563456 123123 35734562 34526 3452 346

123412 51341 12341 473567 11234 45746 578957 23523 3452 32435 3 32452346 

23453 23462 234532 54245 345 4563 6573456 23452345 367 4523 45 2345234

NOT NEEDED

324123 6462 345 3563 67 566 123 412343 4645 76568 5623 5341 23413 65

573568767 345 2354 324623 452 346 2345 234 526 23 4523 452 345 3254 345 

WAVELENGTH

123 234 5134 234 6246 1234 5623 3 568 3245 8 2455 345 47 2345 2

2354 46 5657 24455 1345 4566 3 2345 456 6 345 25 34 2354236 2345

到目前为止,这是我的代码:


with open("Test_File.txt") as data:

    data = infile.readlines()


data = [x.strip() for x in data]    


n = 1000000


list = []


for item in data:

    if item == "TEMPERATURE":

        list.append(item)

        n = 0

        continue

    elif n < 4:   

        list.append(item)

        n += 1

        continue

    elif n >= 4:

        break



print(list)        

当我尝试运行它时,我不断收到错误,所以任何帮助将不胜感激!谢谢!


动漫人物
浏览 204回答 2
2回答

蓝山帝景

您可以使用f.next()或f.__next__()扫描并找到“温度”行,然后附加以下 3 行数据:Python3:l = []with open("Test_File.txt", "r+") as f:&nbsp; &nbsp; while f.__next__().strip() != 'TEMPERATURE':&nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; for _ in range(3):&nbsp; &nbsp; &nbsp; &nbsp; l.append(f.__next__().strip())print(l)>> ['11223 112312 4123123 6423 123124 563456 123123 35734562 34526 3452 346',&nbsp;&nbsp; &nbsp; '123412 51341 12341 473567 11234 45746 578957 23523 3452 32435 3 32452346',&nbsp;&nbsp; &nbsp; '23453 23462 234532 54245 345 4563 6573456 23452345 367 4523 45 2345234']Python2:l = []with open("Test_File.txt", "r") as f:&nbsp; &nbsp; while f.next().strip() != 'TEMPERATURE':&nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; for _ in range(3):&nbsp; &nbsp; &nbsp; &nbsp; l.append(f.next().strip())print(l)>> ['11223 112312 4123123 6423 123124 563456 123123 35734562 34526 3452 346',&nbsp;&nbsp; &nbsp; '123412 51341 12341 473567 11234 45746 578957 23523 3452 32435 3 32452346',&nbsp;&nbsp; &nbsp; '23453 23462 234532 54245 345 4563 6573456 23452345 367 4523 45 2345234']

交互式爱情

import osa = """NOT IMPORTANT234123 1523 1234 613 1234 146134 51234 123231 123 1235123512&nbsp;5467 3 564 245 26 234 5 62 435 234 534 62 345 2346 234 52 345 2345 2456 2345 2362 3452 346 2345 236 254 24 523 45 23462 345 234 54326 23TEMPERATURE11223 112312 4123123 6423 123124 563456 123123 35734562 34526 3452 346123412 51341 12341 473567 11234 45746 578957 23523 3452 32435 3 32452346&nbsp;23453 23462 234532 54245 345 4563 6573456 23452345 367 4523 45 2345234NOT NEEDED324123 6462 345 3563 67 566 123 412343 4645 76568 5623 5341 23413 65573568767 345 2354 324623 452 346 2345 234 526 23 4523 452 345 3254 345&nbsp;WAVELENGTH123 234 5134 234 6246 1234 5623 3 568 3245 8 2455 345 47 2345 22354 46 5657 24455 1345 4566 3 2345 456 6 345 25 34 2354236 2345"""def givedata(dataset, word, lines):&nbsp; &nbsp; b = ""&nbsp; &nbsp; x = dataset.splitlines()&nbsp; &nbsp; for line in x:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if word in line:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for y in range(1, lines+1):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b += x[x.index(line)+y]+ "\n"&nbsp; &nbsp; return bb = givedata(a, "TEMPERATURE", 3)with open("newfile.txt", "w") as file:&nbsp; &nbsp; file.write(b)os.startfile("newfile.txt")输出:11223 112312 4123123 6423 123124 563456 123123 35734562 34526 3452 346123412 51341 12341 473567 11234 45746 578957 23523 3452 32435 3 32452346&nbsp;23453 23462 234532 54245 345 4563 6573456 23452345 367 4523 45 2345234
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python