-5

次のようなファイルがあります。

  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でこれを行うにはどうすればよいですか??

4

3 に答える 3

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
于 2013-06-05T17:35:06.923 に答える
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]]
于 2013-06-05T17:38:18.470 に答える