我有一个看起来像这样的文件:
C 0.210991841737 0.047663662717 0.483960886619
C -0.960965312103 0.688519555303 -0.115848688934
C 1.558206749694 0.047460114159 -0.180267436891
O -0.815251456906 -0.734986972268 -0.138532905591
H 0.223550547304 -0.045637654519 1.567373484970
H -1.759483315918 1.063259112311 0.516330435381
H -0.862256972568 1.177986074652 -1.079897880020
H 2.088617117792 -0.883541600456 0.026321487274
H 2.169362364888 0.872235726981 0.192209317060
H 1.455347299576 0.149768052462 -1.260354487995
我只需要读取数字并将其存储在10x3矩阵中。我怎么在python中做到这一点??
答案 0 :(得分:1)
您可以使用pandas
执行此操作:
In [1]: import pandas as pd
In [2]: df = pd.read_csv(file_name, sep='\s+', header=None)
In [3]: df
Out[3]:
0 1 2 3
0 C 0.210992 0.047664 0.483961
1 C -0.960965 0.688520 -0.115849
2 C 1.558207 0.047460 -0.180267
3 O -0.815251 -0.734987 -0.138533
4 H 0.223551 -0.045638 1.567373
5 H -1.759483 1.063259 0.516330
6 H -0.862257 1.177986 -1.079898
7 H 2.088617 -0.883542 0.026321
8 H 2.169362 0.872236 0.192209
9 H 1.455347 0.149768 -1.260354
答案 1 :(得分:1)
使用numpy
:
>>> np.loadtxt('data.txt', usecols=(1,2,3))
array([[ 0.21099184, 0.04766366, 0.48396089],
[-0.96096531, 0.68851956, -0.11584869],
[ 1.55820675, 0.04746011, -0.18026744],
[-0.81525146, -0.73498697, -0.13853291],
[ 0.22355055, -0.04563765, 1.56737348],
[-1.75948332, 1.06325911, 0.51633044],
[-0.86225697, 1.17798607, -1.07989788],
[ 2.08861712, -0.8835416 , 0.02632149],
[ 2.16936236, 0.87223573, 0.19220932],
[ 1.4553473 , 0.14976805, -1.26035449]])
答案 2 :(得分:0)
>>> with open('abc') as f:
... mat = [map(float,line.split()[1:]) for line in f]
...
>>> mat
[[0.210991841737, 0.047663662717, 0.483960886619],
[-0.960965312103, 0.688519555303, -0.115848688934],
[1.558206749694, 0.047460114159, -0.180267436891],
[-0.815251456906, -0.734986972268, -0.138532905591],
[0.223550547304, -0.045637654519, 1.56737348497],
[-1.759483315918, 1.063259112311, 0.516330435381],
[-0.862256972568, 1.177986074652, -1.07989788002],
[2.088617117792, -0.883541600456, 0.026321487274],
[2.169362364888, 0.872235726981, 0.19220931706],
[1.455347299576, 0.149768052462, -1.260354487995]]