我有一个包含数据的文本文件。它包含了我需要的更多信息,因此我尝试仅提取标题为“ TEMPERATURE”的有关温度的部分。我需要提取三行数据,然后最终仅使用相关数据创建一个新的文本文件。
以下是文本文件“ 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)
当我尝试运行该错误时,我总是遇到错误,因此,我们将不胜感激!谢谢!
答案 0 :(得分:2)
您可以使用f.next()
或f.__next__()
扫描并找到“ TEMPERATURE”行,然后附加以下三行数据:
Python3:
l = []
with open("Test_File.txt", "r+") as f:
while f.__next__().strip() != 'TEMPERATURE':
continue
for _ in range(3):
l.append(f.__next__().strip())
print(l)
>> ['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']
Python2:
l = []
with open("Test_File.txt", "r") as f:
while f.next().strip() != 'TEMPERATURE':
continue
for _ in range(3):
l.append(f.next().strip())
print(l)
>> ['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']
答案 1 :(得分:0)
<div>
import os
a = """
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"""
def givedata(dataset, word, lines):
b = ""
x = dataset.splitlines()
for line in x:
if word in line:
for y in range(1, lines+1):
b += x[x.index(line)+y]+ "\n"
return b
b = givedata(a, "TEMPERATURE", 3)
with open("newfile.txt", "w") as file:
file.write(b)
os.startfile("newfile.txt")