Masih Bingung Kelola File .csv Dengan Python?

Masih Bingung Kelola File .csv Dengan Python

File .csv adalah sesuatu hal yang biasa digunakan untuk melakukan penyimpanan data atau pertukaran data. Misalkan contoh file .csv seperti dibawah ini :

first_name,last_name,gender,age,visit_date,website_goal
James,Mcguire,male,50.0,2017-10-26,Find a job
John,Vaughan,male,35.0,2017-11-09,Pay a bill
Robert,Dillon,male,33.0,2017-11-28,Pay a bill
Michael,Cameron,male,40.0,2017-09-27,When is trash day?
William,Yang,male,38.0,2017-11-01,Contact City Council
David,Trujillo,male,31.0,2017-10-24,Find a job
Richard,Koch,male,28.0,2017-10-12,Contact City Council
Charles,French,male,14.0,2017-09-11,Find a job
Joseph,Humphrey,male,50.0,2017-10-27,Find a job
Thomas,Strickland,male,46.0,2017-10-06,Find a job
Christopher,Waller,male,65.0,2017-09-19,When is trash day?
Daniel,Stephenson,male,29.0,2017-09-05,Find a job
Paul,Berg,male,39.0,2017-10-16,When is trash day?
Mark,Townsend,male,52.0,2017-09-04,Pay a bill
Donald,Schneider,male,27.0,2017-12-24,Contact City Council

Bagaimana pengolahannya dengan menggunakan Python? Ada dua cara yaitu TANPA menggunakan modul .csv dan menggunakan modul .csv.

Tanpa Modul CSV

Berikut ini pengelolaan file .csv tanpa menggunakan modul csv yang dimiliki oleh Python.

# penentuan lokasi file .csv
path = "page_visits_2.csv"
# menyimpan data
lines = [line for line in open(path)]

print("Bagian 1 - Menampilkan Sebagian Data")
# sama dengan data asli
print(lines[1].strip())
# dirubah kedalam bentuk list
print(lines[1].strip().split(','))


print("\nBagian 2 - Menampilkan Semua Data Asli")
for line in lines:
    # tampil seperti data asli
    print(line.strip())

print("\nBagian 3 - Menampilkan Semua Data Dalam Bentuk List")
# semua data dirubah kedalam bentuk list
dataset = [line.strip().split(',') for line in open(path)]
for data in dataset:
    print(data)

Berikut ini hasil keluaran kode program diatas

Bagian 1 - Menampilkan Sebagian Data
James,Mcguire,male,50.0,2017-10-26,Find a job
['James', 'Mcguire', 'male', '50.0', '2017-10-26', 'Find a job']

Bagian 2 - Menampilkan Semua Data Asli
first_name,last_name,gender,age,visit_date,website_goal
James,Mcguire,male,50.0,2017-10-26,Find a job
John,Vaughan,male,35.0,2017-11-09,Pay a bill
Robert,Dillon,male,33.0,2017-11-28,Pay a bill Michael,Cameron,male,40.0,2017-09-27,When is trash day?
William,Yang,male,38.0,2017-11-01,Contact City Council
David,Trujillo,male,31.0,2017-10-24,Find a job
Richard,Koch,male,28.0,2017-10-12,Contact City Council
Charles,French,male,14.0,2017-09-11,Find a job
Joseph,Humphrey,male,50.0,2017-10-27,Find a job
Thomas,Strickland,male,46.0,2017-10-06,Find a job
Christopher,Waller,male,65.0,2017-09-19,When is trash day?
Daniel,Stephenson,male,29.0,2017-09-05,Find a job
Paul,Berg,male,39.0,2017-10-16,When is trash day?
Mark,Townsend,male,52.0,2017-09-04,Pay a bill
Donald,Schneider,male,27.0,2017-12-24,Contact City Council

Bagian 3 - Menampilkan Semua Data Dalam Bentuk List
['first_name', 'last_name', 'gender', 'age', 'visit_date', 'website_goal']
['James', 'Mcguire', 'male', '50.0', '2017-10-26', 'Find a job']
['John', 'Vaughan', 'male', '35.0', '2017-11-09', 'Pay a bill']
['Robert', 'Dillon', 'male', '33.0', '2017-11-28', 'Pay a bill']
['Michael', 'Cameron', 'male', '40.0', '2017-09-27', 'When is trash day?']
['William', 'Yang', 'male', '38.0', '2017-11-01', 'Contact City Council']
['David', 'Trujillo', 'male', '31.0', '2017-10-24', 'Find a job']
['Richard', 'Koch', 'male', '28.0', '2017-10-12', 'Contact City Council']
['Charles', 'French', 'male', '14.0', '2017-09-11', 'Find a job']
['Joseph', 'Humphrey', 'male', '50.0', '2017-10-27', 'Find a job']
['Thomas', 'Strickland', 'male', '46.0', '2017-10-06', 'Find a job'] ['Christopher', 'Waller', 'male', '65.0', '2017-09-19', 'When is trash day?']
['Daniel', 'Stephenson', 'male', '29.0', '2017-09-05', 'Find a job']
['Paul', 'Berg', 'male', '39.0', '2017-10-16', 'When is trash day?']
['Mark', 'Townsend', 'male', '52.0', '2017-09-04', 'Pay a bill']
['Donald', 'Schneider', 'male', '27.0', '2017-12-24', 'Contact City Council']

Berhasil membaca data dari file .csv kemudian merubahnya kedalam bentuk list dan menampilkannya. Akan tetapi masih terdapat permasalahan disini yaitu semua data semua berformat string. Agar didapatkan tipe data yang sesuai maka diperlukan penggunaan modul .csv dari Python.

Be the first to comment

Leave a Reply

Your email address will not be published.


*