gpt4 book ai didi

python - 更改文本中的两行

转载 作者:行者123 更新时间:2023-11-30 23:42:42 25 4
gpt4 key购买 nike

我有一个 python 脚本,到目前为止主要是为我目前正在开发的一个项目编写的,但遇到了障碍。我本质上运行一个程序,输出以下输出文件(称为 big.dmp):

)O+_05 Big-body initial data  (WARNING: Do not delete this line!!)
) Lines beginning with `)' are ignored.
)---------------------------------------------------------------------
style (Cartesian, Asteroidal, Cometary) = Cartesian
epoch (in days) = 1365250.
)---------------------------------------------------------------------
COMPSTAR r=5.00000E-01 d=3.00000E+00 m= 0.160000000000000E+01
4.570923967127310E-01 1.841433531828977E+01 0.000000000000000E+00
-6.207379670518027E-03 1.540861575481520E-04 0.000000000000000E+00
0.000000000000000E+00 0.000000000000000E+00 0.000000000000000E+00

现在,有了这个文件,我需要编辑纪元行和以 COMPSTAR 开头的行,同时保持其余信息从积分到积分不变,因为最后 3 行包含我的对象的笛卡尔坐标,本质上就是程序正在输出。

我知道如何使用 f = open('big.dmp', 'w')f.write('text here') 创建初始文件,但如何将最后三行读入新的 big.dmp 文件以进行下一次集成?

最佳答案

也许是这样的?

infile = open('big1.dmp')
outfile = open('big2.dmp', 'w')

for line in infile:
if line.startswith(')'):
# ignore comments
pass
elif 'epoch' in line:
# do something with line
line = line.replace('epoch', 'EPOCH')
elif line.startswith('COMPSTAR'):
# do something with line
line = line.replace('COMPSTAR', 'comparison star')
outfile.write(line)

关于python - 更改文本中的两行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11269440/

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