用python讀CSV表格的方法

這篇文章主要介紹用python讀CSV表格的方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)建站是一家專業(yè)提供黃平企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、成都網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)、H5高端網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為黃平眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。

用python讀CSV表格的方法:

1、使用PythonI/O讀取csv文件

使用python I/O方法進(jìn)行讀取時(shí)即是新建一個(gè)List 列表然后按照先行后列的順序(類似C語言中的二維數(shù)組)將數(shù)據(jù)存進(jìn)空的List對(duì)象中,如果需要將其轉(zhuǎn)化為numpy 數(shù)組也可以使用np.array(List name)進(jìn)行對(duì)象之間的轉(zhuǎn)化。

birth_data = []
with open(birth_weight_file) as csvfile:
    csv_reader = csv.reader(csvfile)  # 使用csv.reader讀取csvfile中的文件
    birth_header = next(csv_reader)  # 讀取第一行每一列的標(biāo)題
    for row in csv_reader:  # 將csv 文件中的數(shù)據(jù)保存到birth_data中
        birth_data.append(row)

birth_data = [[float(x) for x in row] for row in birth_data]  # 將數(shù)據(jù)從string形式轉(zhuǎn)換為float形式

birth_data = np.array(birth_data)  # 將list數(shù)組轉(zhuǎn)化成array數(shù)組便于查看數(shù)據(jù)結(jié)構(gòu)
birth_header = np.array(birth_header)
print(birth_data.shape)  # 利用.shape查看結(jié)構(gòu)。
print(birth_header.shape)
#
# (189, 9)
# (9,)

2、使用Pandas讀取CSV文件

import pandas as pd

csv_data = pd.read_csv('birth_weight.csv')  # 讀取訓(xùn)練數(shù)據(jù)
print(csv_data.shape)  # (189, 9)
N = 5
csv_batch_data = csv_data.tail(N)  # 取后5條數(shù)據(jù)
print(csv_batch_data.shape)  # (5, 9)
train_batch_data = csv_batch_data[list(range(3, 6))]  # 取這20條數(shù)據(jù)的3到5列值(索引從0開始)
print(train_batch_data)

#      RACE  SMOKE  PTL
# 184   0.0    0.0  0.0
# 185   0.0    0.0  1.0
# 186   0.0    1.0  0.0
# 187   0.0    0.0  0.0
# 188   0.0    0.0  1.0

3、使用Tensorflow讀取CSV文件

'''使用Tensorflow讀取csv數(shù)據(jù)'''
filename = 'birth_weight.csv'
file_queue = tf.train.string_input_producer([filename])  # 設(shè)置文件名隊(duì)列,這樣做能夠批量讀取文件夾中的文件
reader = tf.TextLineReader(skip_header_lines=1)  # 使用tensorflow文本行閱讀器,并且設(shè)置忽略第一行
key, value = reader.read(file_queue)
defaults = [[0.], [0.], [0.], [0.], [0.], [0.], [0.], [0.], [0.]]  # 設(shè)置列屬性的數(shù)據(jù)格式
LOW, AGE, LWT, RACE, SMOKE, PTL, HT, UI, BWT = tf.decode_csv(value, defaults)
# 將讀取的數(shù)據(jù)編碼為我們設(shè)置的默認(rèn)格式
vertor_example = tf.stack([AGE, LWT, RACE, SMOKE, PTL, HT, UI])  # 讀取得到的中間7列屬性為訓(xùn)練特征
vertor_label = tf.stack([BWT])  # 讀取得到的BWT值表示訓(xùn)練標(biāo)簽

# 用于給取出的數(shù)據(jù)添加上batch_size維度,以批處理的方式讀出數(shù)據(jù)??梢栽O(shè)置批處理數(shù)據(jù)大小,是否重復(fù)讀取數(shù)據(jù),容量大小,隊(duì)列末尾大小,讀取線程等屬性。
example_batch, label_batch = tf.train.shuffle_batch([vertor_example, vertor_label], batch_size=10, capacity=100, min_after_dequeue=10)

# 初始化Session
with tf.Session() as sess:
    coord = tf.train.Coordinator()  # 線程管理器
    threads = tf.train.start_queue_runners(coord=coord)
    print(sess.run(tf.shape(example_batch)))  # [10  7]
    print(sess.run(tf.shape(label_batch)))  # [10  1]
    print(sess.run(example_batch)[3])  # [ 19.  91.   0.   1.   1.   0.   1.]
    coord.request_stop()
    coord.join(threads)

'''
對(duì)于使用所有Tensorflow的I/O操作來說開啟和關(guān)閉線程管理器都是必要的操作
with tf.Session() as sess:
    coord = tf.train.Coordinator()  # 線程管理器
    threads = tf.train.start_queue_runners(coord=coord)
    #  Your code here~
    coord.request_stop()
    coord.join(threads)
'''

以上是用python讀CSV表格的方法的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)站題目:用python讀CSV表格的方法
分享路徑:http://muchs.cn/article12/jojjdc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊用戶體驗(yàn)、搜索引擎優(yōu)化響應(yīng)式網(wǎng)站、網(wǎng)站導(dǎo)航、動(dòng)態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化