TensorFlow > file read > 8列持つファイルの4列だけ読む > 後ろはダミー文字列として読んでおく

```txt:動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 14.04 LTS desktop amd64
TensorFlow v0.11
cuDNN v5.1 for Linux
CUDA v8.0
Python 2.7.6
IPython 5.1.0 -- An enhanced Interactive Python.
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
GNU bash, version 4.3.8(1)-release (x86_64-pc-linux-gnu)
```

### input.csv

```input.csv
1,2,3,4,5,6,7,8
1,2,3,4,5,6,7,8
1,2,3,4,5,6,7,8
1,2,3,4,5,6,7,8
1,2,3,4,5,6,7,8
```

上記のファイルのうち４列だけ使いたいということができるかどうか。

### code v0.1

```learnExr_170422.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
import tensorflow as tf
import tensorflow.contrib.slim as slim
import numpy as np

filename_queue = tf.train.string_input_producer(["input.csv"])

# prase CSV
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
input1, input2, output1, output2 = tf.decode_csv(
    value, record_defaults=[[0.], [0.], [0.], [0.]])
inputs = tf.pack([input1, input2])
output = tf.pack([output1, output2])

init_op = tf.initialize_all_variables()
with tf.Session() as sess:
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    res = sess.run(input1)
    print(res)

    coord.join(threads)
```

```txt:run
InvalidArgumentError (see above for traceback): Expect 4 fields but have 8 in record 0
	 [[Node: DecodeCSV = DecodeCSV[OUT_TYPE=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT], field_delim=",", _device="/job:localhost/replica:0/task:0/cpu:0"](ReaderRead:1, DecodeCSV/record_defaults_0, DecodeCSV/record_defaults_1, DecodeCSV/record_defaults_2, DecodeCSV/record_defaults_3)]]
```

４列読むように記述しているけど８列あります、というエラーが出る。

### code v0.2

とりあえずダミー文字列として読むようにしたらエラーは回避できた。

```learnExr_170422.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
import tensorflow as tf
import tensorflow.contrib.slim as slim
import numpy as np

filename_queue = tf.train.string_input_producer(["input.csv"])

# prase CSV
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
input1, input2, output1, output2, dmy1, dmy2, dmy3, dmy4 = tf.decode_csv(
    value, record_defaults=[[0.], [0.], [0.], [0.],[""],[""],[""],[""]])
inputs = tf.pack([input1, input2])
output = tf.pack([output1, output2])

init_op = tf.initialize_all_variables()
with tf.Session() as sess:
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    res = sess.run(input1)
    print(res)

    coord.join(threads)
```


### code v0.3

v0.2と同じだが、読みやすいかと思う。

```learnExr_170422.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
import tensorflow as tf
import tensorflow.contrib.slim as slim
import numpy as np

filename_queue = tf.train.string_input_producer(["input.csv"])

# prase CSV
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)

def_rec = [[0.], [0.], [0.], [0.],[""],[""],[""],[""]]
res = tf.decode_csv(value, record_defaults=def_rec)
input1, input2, output1, output2, dmy1, dmy2, dmy3, dmy4 = res

inputs = tf.pack([input1, input2])
output = tf.pack([output1, output2])

init_op = tf.initialize_all_variables()
with tf.Session() as sess:
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    res = sess.run(input1)
    print(res)

    coord.join(threads)
```