Python Tutorial 21 : How to read/write a CSV file in Python?

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:

  1. Import the CSV library
  2. Use the open function to open an existing file or to create a new file
  3. read from the file or write to the file
  4. Close the file.

How to create a CSV File in Python?

Following example illustrated how to create a telephone book using a CSV file.

How to create CSV file in Python

Following is the key steps:

  1. Import the CSV library[vtftable cols=”{0}0:fff2cc;{/}”]
    import csv;nn;
    [/vtftable]
  2. Define the function called “Phone _Name” where accept the file name as the criteria[vtftable cols=”{0}0:fff2cc;{/}”]
    def phone_name(filename):
    [/vtftable]
  3. Open the input file as write mode and newline = ””[vtftable cols=”{0}0:fff2cc;{/}”]
    f = open(filename,’w’, newline =”)
    [/vtftable]
  4. 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]
  5. 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.
  6. Close the input file[vtftable cols=”{0}0:fff2cc;{/}”]
    f.close()
    [/vtftable]

Following is the input:

How to create CSV file in Python - 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.

How to read a CSV file in Python

Following is the key steps:

  1. Import the CSV library[vtftable cols=”{0}0:fff2cc;{/}”]
    import csv;nn;
    [/vtftable]
  2. 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]
  3. Open the input file as read mode[vtftable cols=”{0}0:fff2cc;{/}”]
    f = open(filename)
    [/vtftable]
  4. Define the column header (as it was not stored in the file)[vtftable cols=”{0}0:fff2cc;{/}”]
    content = [[“Customer Name”,”, Telephone”]]
    [/vtftable]
  5. 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
  6. 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()How to read a CSV file in Python - Result
    [/vtftable]
  7. Close the input file[vtftable cols=”{0}0:fff2cc;{/}”]
    f.close()
    [/vtftable]

Following is the result:

How to read a CSV file in Python - Result

Leave a Comment