將 CSV 檔案匯入 Python 清單
將 CSV 檔案匯入 Python 清單中是一項常見任務。在本文中,我們將示範如何使用 csv 模組來完成此操作。
方法:
範例:
範例:
This is the first line,Line1 This is the second line,Line2 This is the third line,Line3
import csv with open('file.csv', newline='') as f: reader = csv.reader(f) data = list(reader) print(data)
[['This is the first line', 'Line1'], ['This is the second line', 'Line2'], ['This is the third line', 'Line3']]
範例:
with open('file.csv', newline='') as f: reader = csv.reader(f) data = [tuple(row) for row in reader]
[('This is the first line', 'Line1'), ('This is the second line', 'Line2'), ('This is the third line', 'Line3')]
範例:
import csv with open('file.csv', 'rb') as f: reader = csv.reader(f) your_list = list(reader) print your_list
以上是如何在 Python 中將 CSV 檔案匯入清單?的詳細內容。更多資訊請關注PHP中文網其他相關文章!