From my previous post, I have illustrated how to use create text file in Python. What about a CSV file? How to read/write a CSV file in Python?
What is CSV File?
CSV (Comma Separated Values) file is the most common import an export format for spreedsheets and databases. They are actually a text file so that you can even use a notepad to view or create the file. However they are so-called CSV file is because they are usually separate each columns from a database by a comma. However, it can also be separated by using other characters such as a “|” or a “tab”.
How to manipulate a CSV File in Python?
Following is the steps to manipulate a CSV file , in python:
- Import the CSV library
- Use the open function to open an existing file or to create a new file
- read from the file or write to the file
- Close the file.
How to create a CSV File in Python?
Following example illustrated how to create a telephone book using a CSV file.
Following is the key steps:
- Import the CSV library[vtftable cols=”{0}0:fff2cc;{/}”]
import csv;nn;
[/vtftable] - Define the function called “Phone _Name” where accept the file name as the criteria[vtftable cols=”{0}0:fff2cc;{/}”]
def phone_name(filename):
[/vtftable] - Open the input file as write mode and newline = ””[vtftable cols=”{0}0:fff2cc;{/}”]
f = open(filename,’w’, newline =”)
[/vtftable] - Accept input from user[vtftable cols=”{0}0:fff2cc;{/}”]
Customer_name = input(“Please enter Cutomer name. Please retrn to end: “)
if Customer_name ==””:
break
customer_number = input(“Plase enter Customer Number: “)
row =[]
row.append(Customer_name)
row.append(customer_number)
[/vtftable] - Write the new record into the phone book[vtftable cols=”{0}0:fff2cc;{/}”]
csv.writer(f).writerow(row)
[/vtftable]Repeat until the user press enter. - Close the input file[vtftable cols=”{0}0:fff2cc;{/}”]
f.close()
[/vtftable]
Following is the input:
Before review the result, let’s illustrate how to read from a CSV file.
How to read from a CSV File in Python?
Following example illustrated how to red the telephone book CSV file that we have created from our previous example.
Following is the key steps:
- Import the CSV library[vtftable cols=”{0}0:fff2cc;{/}”]
import csv;nn;
[/vtftable] - Define the function called “Phone read_phone_book” where accept the file name as the criteria[[vtftable cols=”{0}0:fff2cc;{/}”]
def read_phone_book(filename):
[/vtftable] - Open the input file as read mode[vtftable cols=”{0}0:fff2cc;{/}”]
f = open(filename)
[/vtftable] - Define the column header (as it was not stored in the file)[vtftable cols=”{0}0:fff2cc;{/}”]
content = [[“Customer Name”,”, Telephone”]]
[/vtftable] - Use the CSV file reader to read the input file and append each row in to the list “content”[vtftable cols=”{0}0:fff2cc;{/}”]
for row in csv.reader(f):
content.append(row)
[/vtftable]Repeat until the end of the file - Print the result[vtftable cols=”{0}0:fff2cc;{/}”]
for p_row in content:
for i in range(0,len(p_row), 1):
print(p_row[i], end = ‘, ‘)
print()
[/vtftable] - Close the input file[vtftable cols=”{0}0:fff2cc;{/}”]
f.close()
[/vtftable]
Following is the result: