TensorFlow 使用Dataset读取CSV数据 (启用Eager)

1. 导入模块 并且屏蔽warning

import tensorflow as tf
import tensorflow.contrib.eager as tfe
import glob
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"

2.启用eager

tf.enable_eager_execution()

3.定义读取数据函数

def csvread(filelist):
    list1 = (tf.data.TextLineDataset(filelist)
             .map(lambda text: tf.string_split([text],delimiter=",").values))  # 使用逗号分隔符

    iterator = tfe.Iterator(list1)
    for i in iterator:
        print(i)

4.获得csv文件数据并运行

if __name__ == '__main__':
    filelist = [i for i in glob.glob("data/*.csv")]
    csvread(filelist=filelist)

完整代码

#!/usr/bin/python
# -*- coding: UTF-8 -*-
__author__ = "HeliantHuS"
__version__ = "1.0"
"""
@ Author: HeliantHuS
@ Codes are far away from bugs with the animal protecting
@ Time:  2019/6/12
@ FileName: read_csvfiletest.py
"""
import tensorflow as tf
import tensorflow.contrib.eager as tfe
import glob

import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"

tf.enable_eager_execution()

def csvread(filelist):
    list1 = (tf.data.TextLineDataset(filelist)
                  .map(lambda text: tf.string_split([text],delimiter=",").values))  # 使用逗号分隔符
    print(type(list1))

    iterator = tfe.Iterator(list1)
    for i in iterator:
        print(i)


if __name__ == '__main__':
    filelist = [i for i in glob.glob("data/*.csv")]
    csvread(filelist=filelist)

你可能感兴趣的:(Python)