我有多个名为ParticleCoordW_10000.dat、ParticleCoordW_20000.dat等的文本文件。。。文件都是这样的:
ITEM: TIMESTEP
10000
ITEM: NUMBER OF ATOMS
1000
ITEM: BOX BOUNDS pp pp pp
0.0000000000000000e+00 9.4000000000000004e+00
0.0000000000000000e+00 9.4000000000000004e+00
0.0000000000000000e+00 9.4000000000000004e+00
ITEM: ATOMS id x y z
673 1.03559 0.495714 0.575399
346 2.74458 1.30048 0.0566235
991 0.570383 0.589025 1.44128
793 0.654365 1.33452 1.91347
969 0.217201 0.6852 0.287291
.
.
.
.
我想用python来提取单个粒子的坐标,比如ATOM ID:673。问题是ATOM ID:673的行位置在每个文本文件中都会发生变化。所以我希望Python能够在目录的每个文本文件中找到ATOM#673,并保存相关联的xyz坐标。在
以前我用这样的方法来获得所有坐标:
^{pr2}$有没有办法修改这个脚本来执行前面描述的任务?在
编辑: 基于各种评论,我写了以下内容:
coord = []
filenames = natsort.natsorted(glob.glob('*.dat'))
for f in filenames:
buff = open(f, 'r').readlines()
for row in buff:
if row.startswith('673'):
coord.append(row)
np.savetxt("xyz.txt",coord,fmt,delimiter=' ')
这样我就可以在目录中的所有文本文件中对单个粒子的所有坐标进行分组。但是我想对所有的粒子ID(1000个粒子)完成这个过程。最有效的方法是什么?在
如果没有更多的背景知识,我无法想象没有一种方法可以在不读取Atom Id所在的行的情况下找到正确的行。在
你会做一些类似的事情:
^{1}$否则,您可以保存/读取每个文件的“Mapping”ID<;->;行号
不过,我认为你应该想出一个办法,以有序的方式保存这些职位。也许你也可以在你的问题中给出信息,是什么阻止你保存由Atom ID排序的位置
我建议使用hdf5 library来存储包含元数据的大型数据集。
您可以使用正则表达式从所有文件中获取数据,然后根据需要处理它们。像这样的事情可能会奏效。在
^{1}$这将为您提供一个以
^{pr2}$ATOM ID
为键、所有坐标列表为值的字典。 输出示例:在回顾这个问题后,我认为我误解了输入。 在所有文件中,
ITEM: ATOMS id x y z
行是静态的。所以,我把代码改了一点。在相关问题 更多 >
编程相关推荐