Python讀寫csv檔案的操作方法

PHPz
發布: 2023-06-26 13:09:59
轉載
6141 人瀏覽過

要在 Python 中寫入 CSV,請使用 Python 的 csv 模組。

例如,讓我們將字串清單寫入一個新的 CSV 檔案:

import csv data = ["This", "is", "a", "Test"] with open('example.csv', 'w') as file: writer = csv.writer(file) writer.writerow(data)
登入後複製
登入後複製

因此,您會在目前資料夾中看到一個名為 example.csv 的檔案。

用Python 寫CSV 的4 個步驟

要在Python 中寫入CSV 檔案:

1.以寫入模式開啟CSV 檔案。這是使用 open() 函數發生的。給它檔案的路徑作為第一個參數。將模式指定為第二個參數(“r”表示讀取,“w”表示寫入)。
2.建立 CSV 編寫器物件。為此,建立一個 csv 模組的 writer() 對象,並將開啟的檔案作為其參數傳遞。
3.將資料寫入 CSV 檔案。使用 writer 物件的 writerow() 函數將資料寫入 CSV 檔案。
4.關閉 CSV 檔案,使用檔案的 close() 方法。

這是一個說明此過程的範例:

import csv # 1. file = open('test.csv', 'w') # 2. writer = csv.writer(file) # 3. data = ["This", "is", "a", "Test"] writer.writerow(data) # 4. file.close()
登入後複製

這段程式碼在目前資料夾中建立了一個名為 test.csv 的檔案。

如果指定的檔案不存在,open() 函數將會開啟一個新檔案。如果是,則開啟現有文件。

速寫

要縮短寫入 CSV 的時間,請使用with語句開啟檔案。這樣你就不用擔心自己關閉文件了。with會自動處理該部分。

例如:

import csv # 1. step with open('test.csv', 'w') as file: # 2. step writer = csv.writer(file) # 3. step data = ["This", "is", "a", "Test"] writer.writerow(data)
登入後複製

這將在當前資料夾中建立一個名為 test.csv 的新 CSV 文件,並將字串清單寫入其中。

如何在 Python 中將非 ASCII 字元寫入 CSV

預設情況下,您無法將非 ASCII 字元寫入 CSV 檔案。

要支援將非 ASCII 值寫入 CSV 文件,請在 open() 呼叫中將字元編碼指定為第三個參數。

with open('PATH_TO_FILE.csv', 'w', encoding="UTF8")
登入後複製

其餘過程遵循您之前學到的步驟。

如何為 CSV 檔案建立標題

到目前為止,您已經建立了缺少結構的 CSV 檔案。

在 Python 中,可以使用用於將任何資料寫入 CSV

writerow() 函數為任何 CSV 檔案編寫標頭。

範例: 讓我們建立一個包含學生資料的範例 CSV 檔案。

為了有效地建立數據,需要在CSV檔案的開頭為學生創建一個標題並將其插入。您可以按照先前相同的步驟將資料寫入CSV文件,這樣就可以完成操作。

這是程式碼:

import csv # Define the structure of the data student_header = ['name', 'age', 'major', 'minor'] # Define the actual data student_data = ['Jack', 23, 'Physics', 'Chemistry'] # 1. Open a new CSV file with open('students.csv', 'w') as file: # 2. Create a CSV writer writer = csv.writer(file) # 3. Write data to the file writer.writerow(student_header) writer.writerow(student_data)
登入後複製

這會將 students.csv 檔案建立到您目前正在使用的資料夾中。新檔案如下所示:

Python讀寫csv檔案的操作方法

如何在Python 中將多行寫入CSV 檔案

在Python 中,您可以使用CSV 編寫器的writerows () 函數同時將多行寫入CSV 檔案。

例子。假設您要將多行資料寫入 CSV 檔案。例如,您可能有學生列表,而不是只有其中一個。

要將多行資料寫入 CSV,請使用 writerows() 方法。

這是一個例子:

import csv student_header = ['name', 'age', 'major', 'minor'] student_data = [ ['Jack', 23, 'Physics', 'Chemistry'], ['Sophie', 22, 'Physics', 'Computer Science'], ['John', 24, 'Mathematics', 'Physics'], ['Jane', 30, 'Chemistry', 'Physics'] ] with open('students.csv', 'w') as file: writer = csv.writer(file) writer.writerow(student_header) # Use writerows() not writerow() writer.writerows(student_data)
登入後複製

這會產生一個新的CSV 文件,如下所示:

Python讀寫csv檔案的操作方法

如何在Python 中將字典寫入CSV 檔案

使用DictWriter 物件可以實現在Python 中將字典寫入CSV 檔案, 具體包括以下三個步驟:

1. 使用csv 模組的DictWriter 物件並在其中指定欄位名稱。

2. 使用 writeheader() 方法將標頭建立到 CSV 檔案中。

3. 使用 writerows() 方法將字典資料寫入檔案。

範例:讓我們將學生資料字典寫入 CSV 檔案。

import csv student_header = ['name', 'age', 'major', 'minor'] student_data = [ {'name': 'Jack', 'age': 23, 'major': 'Physics', 'minor': 'Chemistry'}, {'name': 'Sophie', 'age': 22, 'major': 'Physics', 'minor': 'Computer Science'}, {'name': 'John', 'age': 24, 'major': 'Mathematics', 'minor': 'Physics'}, {'name': 'Jane', 'age': 30, 'major': 'Chemistry', 'minor': 'Physics'} ] with open('students.csv', 'w') as file: # Create a CSV dictionary writer and add the student header as field names writer = csv.DictWriter(file, fieldnames=student_header) # Use writerows() not writerow() writer.writeheader() writer.writerows(student_data)
登入後複製

現在結果與前面範例中的students.csv 檔案相同:

Python讀寫csv檔案的操作方法

#結論

CSV 或逗號分隔值是一種常用的文件格式。它由通常用逗號分隔的值組成。

要在 Python 中寫入 CSV,您需要透過以下步驟使用 csv 模組:

1. 以寫入模式開啟 CSV 檔案。

2. 建立 CSV 編寫器物件。

3. 將資料寫入 CSV 檔案。

4. 關閉 CSV 檔案。

這是一個實際的例子。

import csv data = ["This", "is", "a", "Test"] with open('example.csv', 'w') as file: writer = csv.writer(file) writer.writerow(data)
登入後複製
登入後複製

編碼愉快!

以上是Python讀寫csv檔案的操作方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!