-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcsv.py
More file actions
43 lines (37 loc) · 1.17 KB
/
csv.py
File metadata and controls
43 lines (37 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# csv 模組可以協助匯入csv檔案
import csv
# newline代表換行字元
with open('test.csv','w',newline='') as csvfile:
writer = csv.writer(csvfile)
# 寫入欄位名稱
writer.writerow(['Name','Height','Weight'])
# 寫入資料
writer.writerow(['A',170,65])
writer.writerow(['B',180,80])
# 以Dictionary寫入csv
import csv
with open('test2.csv','w',newline='') as csvfile:
# 定義欄位
fieldnames = ['Name','Height','Weight']
# 寫入csv
writer = csv.DictWriter(csvfile , fieldnames=fieldnames)
# 寫入欄位名稱
writer.writeheader()
# 寫入資料
writer.writerow({'Name':'Peter' , 'Height':170 , 'Weight':73})
writer.writerow({'Name':'Josh', 'Height': 168, 'Weight': 85})
# 利用reader方法實現csv檔案讀取
import csv
with open('./test2.csv' , 'r' , newline='') as csvfile:
# 讀取檔案內容
rows = csv.reader(csvfile)
# 迴圈顯示每一列
for i in rows:
print(i)
# 將讀取的檔案轉為 dictionary 格式
import csv
with open('./test.csv' , 'r' , newline='') as csvfile:
rows = csv.DictReader(csvfile)
for j in rows:
print(j['Name'] , j['Height'] , j['Weight'])
#