gpt4 book ai didi

python - 如何从文本文件中提取数据并导入到新的输出文件中?

转载 作者:太空宇宙 更新时间:2023-11-04 01:28:20 25 4
gpt4 key购买 nike

假设以下是 ctf_output.txt 中的数据,我想从 Rod #、Surface Temp 和 Centerline Temp 中提取标题标题,然后只提取每个杆的最高温度。请注意,我在每一列中都放置了特定且显着更高的温度,没有重复。

Rod 1  
Surface Temperature Centerline Temperature
500 510
501 511
502 512
503 513
504 525
505 515
535 516
507 517
508 518
509 519
510 520
Rod 2
Surface Temperature Centerline Temperature
500 510
501 511
502 512
503 513
504 555
505 515
540 516
507 517
508 518
509 519
510 520
Rod 3
Surface Temperature Centerline Temperature
500 510
501 511
502 512
503 513
567 514
505 515
506 559
507 517
508 518
509 519
510 520

我怎样才能用 python 做到这一点?我需要一个 python 脚本来提取数据并使用以下格式填充新的输出文件:

Rod 1  
Surface Temperature Centerline Temperature
535 525

Rod 2
Surface Temperature Centerline Temperature
540 555

Rod 3
Surface Temperature Centerline Temperature
567 559

最佳答案

您将逐行读取文件,然后跟踪最大值并在下一节开始时输出它们:

with open('ctf_output.txt', 'r') as temps, open(outputfilename, 'w') as output:
surface_max = centerline_max = None
for line in temps:
if line.startswith('Rod'):
# start of new section
if surface_max is not None or centerline_max is not None:
# write maximum for previous section
output.write('{}\t\t\t{}\n\n'.format(surface_max, centerline_max))
# write out this line and the next to the output file
output.write(line)
output.write(next(temps, ''))
# reset maxima
surface_max = centerline_max = 0
elif line.strip():
# temperature line; read temperatures and track maxima
surface, centerline = [int(t) for t in line.split()]
if surface > surface_max:
surface_max = surface
if centerline > centerline_max:
centerline_max = centerline
if surface_max or centerline_max:
# write out last maxima
output.write('{}\t\t\t{}\n'.format(surface_max, centerline_max))

输出使用 3 个选项卡,就像您的输入一样。

对于您的示例输入,它写道:

Rod 1  
Surface Temperature Centerline Temperature
535 525

Rod 2
Surface Temperature Centerline Temperature
540 555

Rod 3
Surface Temperature Centerline Temperature
567 559

关于python - 如何从文本文件中提取数据并导入到新的输出文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15933708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com