Python程式設計中使用csv檔案
CSV檔案(逗號分隔值檔案)是跨平臺儲存和共享資料的最廣泛使用的平面檔案之一。列以逗號分隔,還有一個可選的標題行,用於指示每列的名稱。Python可以使用許多模組來讀取CSV檔案。在本文中,我們將瞭解如何使用Python中的CSV庫來讀取和寫入CSV檔案。我們還可以看到僅使用pandas庫讀取CSV檔案。
使用csv模組讀取CSV檔案
您可以從(https://www.guru99.com/python-csv.html)獲取CSV檔案
示例
import csv
with open('C:\iris.csv','rt')as file:
csv_rows = csv.reader(file)
for row in csv_rows:
print(row)輸出
執行以上程式碼將給出以下結果:
['sepal.length', 'sepal.width', 'petal.length', 'petal.width', 'variety'] ['5.1', '3.5', '1.4', '.2', 'Setosa'] ['4.9', '3', '1.4', '.2', 'Setosa'] ['4.7', '3.2', '1.3', '.2', 'Setosa'] ['4.6', '3.1', '1.5', '.2', 'Setosa'] ['5', '3.6', '1.4', '.2', 'Setosa'] ['5.4', '3.9', '1.7', '.4', 'Setosa'] ['4.6', '3.4', '1.4', '.3', 'Setosa'] ……………… ……………
使用Pandas讀取CSV檔案
Pandas庫也可以用於讀取csv檔案。它具有用於讀取csv的方法,可以直接應用,繞過路徑和檔名。讀取檔案後,它將成為一個數據集,然後我們可以根據需要列印資料集的不同部分。
示例
import pandas as pd
datainput = pd.read_csv('C:\iris.csv')
print("Given dataset values : \n", datainput)
#size of the dataset
print("\nThe size of the dataset is :\n",datainput.shape)
#printing few rows from the dataset
print("\n printing few rows from the dataset :\n",datainput[0:6])輸出
執行以上程式碼將給出以下結果:
Given dataset values : sepal.length sepal.width petal.length petal.width variety 0 5.1 3.5 1.4 0.2 Setosa 1 4.9 3.0 1.4 0.2 Setosa 2 4.7 3.2 1.3 0.2 Setosa 3 4.6 3.1 1.5 0.2 Setosa 4 5.0 3.6 1.4 0.2 Setosa .. ... ... ... ... ... 145 6.7 3.0 5.2 2.3 Virginica 146 6.3 2.5 5.0 1.9 Virginica 147 6.5 3.0 5.2 2.0 Virginica 148 6.2 3.4 5.4 2.3 Virginica 149 5.9 3.0 5.1 1.8 Virginica [150 rows x 5 columns] The size of the dataset is : (150, 5) printing few rows from the dataset : sepal.length sepal.width petal.length petal.width variety 0 5.1 3.5 1.4 0.2 Setosa 1 4.9 3.0 1.4 0.2 Setosa 2 4.7 3.2 1.3 0.2 Setosa 3 4.6 3.1 1.5 0.2 Setosa 4 5.0 3.6 1.4 0.2 Setosa 5 5.4 3.9 1.7 0.4 Setosa
使用csv模組寫入CSV檔案
要建立csv檔案,我們使用Python列表,我們宣告一個數據集,其中每一行作為一個列表,所有行都是一個大的單一列表中的子列表。我們還建立另一個數據集,它只表示標題行。然後我們使用writerow()和csv.writer等各種方法,最終將檔案寫入本地系統。
示例
import csv data = ["Month", "1958", "1959", "1960"] x = [ ["JAN", 340, 360, 417], ["FEB", 318, 342, 391], ["MAR", 362, 406, 419], ["APR", 348, 396, 461], ["MAY", 363, 420, 472], ["JUN", 435, 472, 535], ["JUL", 491, 548, 622], ["AUG", 505, 559, 606], ["SEP", 404, 463, 508], ["OCT", 359, 407, 461], ["NOV", 310, 362, 390], ["DEC", 337, 405, 432], ] y = "C:\years.csv" with open(y, 'w') as work: z = csv.writer(work) z.writerow(data) z.writerows(x)
輸出
執行以上程式碼將給出以下結果:
Month,1958,1959,1960 JAN,340,360,417 FEB,318,342,391 MAR,362,406,419 APR,348,396,461 MAY,363,420,472 JUN,435,472,535 JUL,491,548,622 AUG,505,559,606 SEP,404,463,508 OCT,359,407,461 NOV,310,362,390 DEC,337,405,432
使用pandas寫入CSV檔案
使用pandas,我們建立一個數據框,其中國家既是行,也是行的標題。然後我們使用to_csv方法,該方法將檔名和路徑作為引數,並將資料寫入csv檔案。
示例
from pandas import DataFrame
C = {'Month': ['JAN','FEB', 'MAR'],
'1958': ['345', '435', '545'],
'1959': ['377', '135', '985'],
'1960': ['498', '354', '765'],
}
df = DataFrame(C, columns= ["Month", "1958", "1959", "1960"])
export_csv = df.to_csv (r'C:\years_p.csv', index = None, header=True) # here you have to write path, where result file will be stored
print (df)輸出
執行以上程式碼將給出以下結果:
Month 1958 1959 1960 0 JAN 345 377 498 1 FEB 435 135 354 2 MAR 545 985 765
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP