從 CSV 讀取資料
使用標準函式庫中的 Python CSV 模組,將資料載入為由字典組成的 list。
你可以回頭參考你做過的[第 4 章練習](https://campus.datacamp.com/courses/introduction-to-relational-databases-in-python/creating-and-manipulating-your-own-databases?ex=7),內容與此相似。
本練習屬於課程
Python 資料庫入門
練習說明
- 建立一個名為
values_list的空 list。 - 以 for 迴圈走訪
csv_reader的各列,對每一列建立一個名為data的字典,並將它加入values_list。- 在 for 迴圈內,
row會是一個 list,其成員依序為'state'、'sex'、'age'、'pop2000'與'pop2008'(順序固定)。
- 在 for 迴圈內,
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create an empty list: values_list
values_list = ____
# Iterate over the rows
for row in csv_reader:
# Create a dictionary with the values
data = {____: row[____], ____: row[____], ____:____, ____: ____,
____: ____}
# Append the dictionary to the values list
values_list.append(____)