Python讀寫csv檔案

2021-06-07 19:26:51 字數 808 閱讀 8700

1. 寫入並生成csv檔案

**:# coding: utf-8

import csv

csvfile = file('csv_test.csv', 'wb')

writer = csv.writer(csvfile)

writer.writerow(['姓名', '年齡', '**'])

data = [

('小河', '25', '1234567'),

('小芳', '18', '789456') ]

writer.writerows(data)

csvfile.close()

2. 讀取csv檔案

**:# coding: utf-8

import csv

csvfile = file('csv_test.csv', 'rb')

reader = csv.reader(csvfile)

for line in reader:

print line

csvfile.close() 

執行結果:

root@he-desktop:~/python/example# python read_csv.py 

['\xe5\xa7\x93\xe5\x90\x8d', '\xe5\xb9\xb4\xe9\xbe\x84', '\xe7\x94\xb5\xe8\xaf\x9d']

['\xe5\xb0\x8f\xe6\xb2\xb3', '25', '1234567']

['\xe5\xb0\x8f\xe8\x8a\xb3', '18', '789456']

python 讀寫csv檔案

1.忽略第一行標題的基礎上 python2.7 coding utf 8 import csv csv reader csv.reader open r c users thinkpad desktop tweets.csv for row in csv reader 條件語句忽略第一行檔案資料 i...

python 讀寫csv檔案

1.將dataframe資料寫入csv 1 用 csv包一行一行的寫入 import csv python2可以用file替代open with open test.csv w as csvfile writer csv.writer csvfile 先寫入columns name writer.w...

python讀寫csv檔案

吳下 阿蒙 csv釋義 逗號分隔值 comma separated values,csv,有時也稱為字元分隔值,因為分隔字元也可以不是逗號 其檔案以純文字形式儲存 資料 數字和文字 純文字意味著該檔案是乙個字串行,不含必須像二進位制數字那樣被解讀的資料。csv檔案由任意數目的記錄組成,記錄間以某種換...