LoginSignup
2
2

More than 5 years have passed since last update.

Numpy > ファイル読込み > 行ごとに列数の異なるファイルを読む > genfromtxt: Line #1729 (got 4 columns instead of 2)の回避

Last updated at Posted at 2018-05-25
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04.4 LTS desktop amd64
TensorFlow v1.7.0
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
scipy v0.19.1
geopandas v0.3.0
MATLAB R2017b (Home Edition)
ADDA v.1.3b6
gnustep-gui-runtime v0.24.0-3.1
PyMieScatt v1.7.0

処理概要

下記のファイルがある。

$ head vtk.out 
# vtk DataFile Version 2.0
gsphere output            
ASCII                     
DATASET POLYDATA          
POINTS     578  float
   0.0000000000000000        0.0000000000000000        1.6484627588210747     
  0.22025970645867510        0.0000000000000000        1.6730385712011135     
   1.2375466764379659E-017  0.20210670983659845        1.5351528724506893     
 -0.19117217165911085        2.3411838810837846E-017   1.4520968091182878     
  -3.9762646348017165E-017 -0.21645776494197605        1.6441599582893061     
$ tail vtk.out 
           3         575         570         569
           3         576         570         575
           3         576         571         570
           3         576         572         571
           3         573         572         576
           3         573         565         572
           3         577         574         573
           3         577         575         574
           3         577         576         575
           3         577         573         576

それぞれの行の数値を読込みたい。

genfromtxt > Error > Line #1729 (got 4 columns instead of 2)

test_read_vtkout_180526.py
import numpy as np
import sys

IN_FILE = 'vtk.out'

dat = np.genfromtxt(IN_FILE, dtype='str')

for idx, elem in enumerate(dat):
    print(elem)
    if idx > 5:
        break

run
$ python3 test_read_vtkout_180526.py
...
    Line #1733 (got 4 columns instead of 2)
    Line #1734 (got 4 columns instead of 2)
    Line #1735 (got 4 columns instead of 2)

項目数が途中で変わるため、上記のエラーが出る。

対処

delimiterを指定して対処した。

test_read_vtkout_180526.py
import numpy as np
import sys

IN_FILE = 'vtk.out'

dat = np.genfromtxt(IN_FILE, dtype='str', delimiter=',')

for idx, elem in enumerate(dat):
    print(elem)
    if idx > 5:
        break

run
$ python3 test_read_vtkout_180526.py 
gsphere output
ASCII
DATASET POLYDATA
POINTS     578  float
0.0000000000000000        0.0000000000000000        1.6484627588210747
0.22025970645867510        0.0000000000000000        1.6730385712011135
1.2375466764379659E-017  0.20210670983659845        1.5351528724506893

後は、自分が使いたい数値を含む文字列に対して.split()して要素インデックスを指定して取り出せばいい。

test_read_vtkout_180526.py
import numpy as np
import sys

'''
v0.1 May, 26, 2018
  - add get_vertices_number()
'''


def get_vertices_number(instr):
    # instr: e.g. 'POINTS    578 float'
    return instr.split()[1]

IN_FILE = 'vtk.out'  # output of [G-sphere]
KEY_VERTICES = 'POINTS'

dat = np.genfromtxt(IN_FILE, dtype='str', delimiter=',')

for elem in dat:
    if KEY_VERTICES in elem:
        numVert = get_vertices_number(elem)
        print(numVert)

$ python3 test_read_vtkout_180526.py 
578

検索用キーワード

  • 任意列数
  • 任意フォーマット
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2