我不明白什么字母O,H,C的意思是如何添加这些信息(我不是化学家)。他们存放在哪里?但是如果你是一个科学家,你可能使用numpy模块。因此,这里是一个简单的解决方案,转你的行三乘三:
import numpy as np
import csv
#assuming your vector is in a text file
vector = np.genfromtxt('yourfilename.txt',dtype='str')
a = np.array(vector)
a = a.reshape((-1, 3))
print(a)
输出:
[['x1' 'y1' 'z1']
['x2' 'y2' 'z2']
['x3' 'y3' 'z3']
['x4' 'y4' 'z4']
['x5' 'y5' 'z5']
['x6' 'y6' 'z6']
['x7' 'y7' 'z7']
['x8' 'y8' 'z8']]
"""
The output is a list of lists in which each element is a row.
Numpy has a simple method to write a 2D array in a csv file :
np.savetxt("youfilename.csv", a, delimiter=",")
but since I get a bug in this moment, I will use a classical method.
"""
with open("output.csv", "w") as f:
writer = csv.writer(f)
writer.writerows(a)
CSV文件:
x1,y1,z1
x2,y2,z2
x3,y3,z3
x4,y4,z4
x5,y5,z5
x6,y6,z6
x7,y7,z7
x8,y8,z8
嗯,我还需要导入的文本文件包含数字(按列向量排序)。 “\ n”是什么意思? – Yoda
@Yoda \ n的意思是“新行”,这是一个字符像任何其他。它们可以被擦除。我将编辑并澄清我的答案。 –