Python Waca Tulis File CSV

Ing kiriman iki, kita bakal nemokake cara maca lan nulis file CSV ing Python. Tuladhane nggunakake modul CSV lan Pandas.



Python Maca File CSV Nggunakake Modul CSV

Contone kode iki maca file sing jenenge orders.csv lan daur ulang liwat data:

import csv f = open('orders.csv', 'rt') orders = csv.reader(f) for order in orders:
print(order) f.close()

Output pesenan.csv:


['OrderID', 'CustomerID', 'OrderDate'] ['10248', '4', '10/02/2020'] ['10249', '2', '10/02/2020'] ['10250', '7', '10/02/2020']

Waca File CSV minangka Kamus

Nggunakake DictReader cara saben baris ing file CSV diwakili minangka baris ing kamus, kanthi header kolom minangka kunci.

import csv orders = csv.DictReader(open('orders.csv')) for order in orders:
print(order)

Output pesenan.csv:


{'OrderID': '10248', 'CustomerID': '4', 'OrderDate': '10/02/2020'} {'OrderID': '10249', 'CustomerID': '2', 'OrderDate': '10/02/2020'} {'OrderID': '10250', 'CustomerID': '7', 'OrderDate': '10/02/2020'}

Waca File CSV Nggunakake Pandas

Kanggo nggunakake Pandas, luwih dhisik kudu nginstal perpustakaan Pandas.



Kanggo nginstal, jalanake printah iki: pip3 install pandas.

import pandas orders = pandas.read_csv('orders.csv') print(orders)

Output pesenan.csv:

OrderID CustomerID OrderDate 10248

4
10/02/2020 10249

2
10/02/2020 10250

7
10/02/2020


Python Tulis CSV Nggunakake Pandas

from pandas import DataFrame import pandas as pd order = pd.DataFrame({'OrderID': ['10251', '10252', '10253'],



'CustomerID': ['5', '1', '8'],



'OrderDate': ['11/02/2020', '11/02/2020', '11/02/2020']}) order.to_csv('newOrders.csv', index=False)

Output newOrders.csv:


OrderID,CustomerID,OrderDate 10251,5,11/02/2020 10252,1,11/02/2020 10253,8,11/02/2020

Nambah Data Kanggo File CSV sing Ana Saiki

Mode default nalika nulis menyang file csv yaiku 'w'. Yen kita pengin nambah data menyang file CSV sing ana, kita kudu nggunakake mode tambahan, kayata. mode='a'

from pandas import DataFrame import pandas as pd order = pd.DataFrame({'OrderID': ['10254'],



'CustomerID': ['3'],



'OrderDate': ['11/02/2020']}) order.to_csv('newOrders.csv', mode='a', index=False, header=False)

Output newOrders.csv:

OrderID,CustomerID,OrderDate 10251,5,11/02/2020 10252,1,11/02/2020 10253,8,11/02/2020 10254,3,11/02/2020

Python Tulis CSV Nggunakake Modul CSV

N.B. Baris kaping pisanan digunakake minangka header kolom

import csv with open('orders.csv', 'w', newline='') as file:
order = csv.writer(file)
order.writerow(['OrderID', 'CustomerID', 'OrderDate'])
order.writerow(['10251', '6', '11/02/2020'])
order.writerow(['10252', '9', '11/02/2020'])
order.writerow(['10253', '5', '11/02/2020'])

Output pesenan.csv:


OrderID,CustomerID,OrderDate 10251,6,11/02/2020 10252,9,11/02/2020 10253,5,11/02/2020

Yen sampeyan pengin nambah file csv sing ana nggunakake modul csv, sampeyan kudu mlebu 'a' paramèter kanggo open() cara Sampeyan uga kudu ngilangi 'judhul'.

Contone:

with open('orders.csv', 'a', newline='') as file:
order = csv.writer(file)
order.writerow(['10251', '6', '11/02/2020'])


Tulis File CSV karo DictWriter

Kita uga bisa nggunakake DictWriter cara kanggo csv kelas kanggo nggawe lan nulis file CSV.

import csv with open('orders.csv', 'w', newline='') as file:
fieldnames = ['OrderID', 'CustomerID', 'OrderDate']
order = csv.DictWriter(file, fieldnames=fieldnames)
order.writeheader()
order.writerow({'OrderID': '10251', 'CustomerID': 7, 'OrderDate': '11/02/2020'})
order.writerow({'OrderID': '10252', 'CustomerID': 3, 'OrderDate': '11/02/2020'})
order.writerow({'OrderID': '10253', 'CustomerID': 1, 'OrderDate': '11/02/2020'})

Output pesenan.csv:


OrderID,CustomerID,OrderDate 10251,7,11/02/2020 10252,3,11/02/2020 10253,1,11/02/2020