我正在尝试使用 .xyz 文件,并且可以打开并读取该文件。我需要为每行 xyz 数据计算 r(距离)(通过执行 r^2 = x^2+y^2+z^2)。但是,我未能将每行中的每个 xyz 值分开,因为它将数据识别为单列 ex([-3.657420e-01 -1.434236e+00 1.086713e+00]) 是单行,理想情况下应该是由 x,y,z 值分隔。
我将脚本附加到打开/显示 xyz 文件和示例输出。谁能帮我计算每行数据的“r”?
谢谢
import numpy as np
filename = "lj-0200.xyz"
xyz_file = np.genfromtxt(fname=filename,skip_header=2,dtype='unicode')
xyz = open(filename)
atom_coordinates = (xyz_file[:,1:])
atom_coordinates = atom_coordinates.astype(float)
atom_quantity = int(xyz.readline())
title = xyz.readline()
print("Input filename is",filename, "Title is", title,"There are",atom_quantity,"atoms in this file")
print(atom_coordinates)
输出
Input filename is lj-0200.xyz Title is A Box of Atoms
There are 200 atoms in this file
[[-3.657420e-01 -1.434236e+00 1.086713e+00]
[ 1.758085e+00 -2.733505e+00 -4.553970e-01]
[-2.777260e+00 8.659730e-01 -9.788090e-01]
[-8.509290e-01 8.053680e-01 -1.050607e+00]
[ 4.697280e-01 -1.262455e+00 1.770134e+00]
[ 1.776954e+00 2.284919e+00 2.691217e+00]
[ 1.564369e+00 2.100387e+00 -3.898060e-01]
[ 2.153646e+00 1.233546e+00 1.145661e+00]
[ 2.373994e+00 2.372510e+00 1.522204e+00]...