到目前为止,我有一个python脚本,主要是为我目前正在进行的一个项目编写的,并且遇到了一个障碍。我基本上运行一个程序,输出以下输出文件(称为大.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')
来创建初始文件,但是如何将最后三行读入一个新文件呢大.dmp下一次集成的文件?你知道吗
下面是一个更能容忍变化的版本:
如果文件的格式与行号有关的固定,则此解决方案将仅更改两行:
生成此输出文件:
显然,如果行号不一致,这将不起作用,但我想我会提供它,以防您的数据格式在行号方面是一致的。你知道吗
而且,由于它一次将整个文件读入内存,因此对于真正的大文件来说,它不是一个理想的解决方案。你知道吗
使用
with
打开文件的好处是,当您处理完文件或遇到异常时,它们会自动关闭。你知道吗有更灵活的解决方案(搜索字符串,逐行处理文件),但如果您的数据是固定的和小的,那么利用这些因素没有坏处。有个聪明人曾经说过“简单胜于复杂”
也许是这样的?你知道吗
相关问题 更多 >
编程相关推荐