How To Read Multiple Columns From CSV File In Python

This article about How To Read Multiple Columns From CSV files in Python is designed in Python programming language.

Python is very easy to research the syntax emphasizes readability and it is able to reduce time ingesting in development.

How To Read CSV File In Python: Project Information

Project Name:How To Read CSV Files in Python
Language/s Used:Python (GUI) Based
Python version (Recommended):2.x or 3.x
Database:None
Type:Python App
Developer:IT SOURCECODE
Updates:0
How To Read CSV File In Python – Project Information

About The Project on How To Read Multiple CSV Files in Python

A Reading CSV File In Python, Also designed in Graphical User Interface (GUI), This article also includes the downloadable Read A CSV File In Python source code for free.

In this tutorial, I will teach you How To Read A CSV File In Python, also this tutorial is the simplest way for beginners or student to Writing CSV File In Python.

To start creating a CSV File In Python, make sure that you have Pycharm IDE installed on your computer.

Recommendations

By the way, if you are new to Python programming and don’t know what Python IDE to use, I have here a list of Best Python IDE for Windows, Linux, and Mac OS that will suit you.

I also have here How to Download and Install Latest Version of Python on Windows.

Steps on How To Read Multiple Columns From CSV Files in Python

Time needed: 5 minutes

How To Read CSV Files in Python With Source Code

  • Step 1: Create a project name.

    First, open Pycharm IDE and click “file.” After that create a project name and then click the “create” button.
    read csv file create project

  • Step 2: Create a python file.

    Second “right-click” your project name folder and choose new and then click “python file“.
    read csv file create python file

  • Step 3: Name your python file.

    Third, name your Python file and click “enter” to start creating CSV File In Python.
    read csv python file name

  • Step 4: The actual code.

    Fourth, the actual coding in creating CSV File In Python, you are free to copy the code and download the full source code given below.

The code given below is for Importing modules.

from tkinter import *
import tkinter.ttk as ttk
import csv

In this part of codes which is importing all the modules given on top.

The code given below is for the design of this project.

root = Tk()
root.title("Python - Import CSV File To Tkinter Table")
width = 500
height = 400
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width/2) - (width/2)
y = (screen_height/2) - (height/2)
root.geometry("%dx%d+%d+%d" % (width, height, x, y))
root.resizable(0, 0)


TableMargin = Frame(root, width=500)
TableMargin.pack(side=TOP)
scrollbarx = Scrollbar(TableMargin, orient=HORIZONTAL)
scrollbary = Scrollbar(TableMargin, orient=VERTICAL)
tree = ttk.Treeview(TableMargin, columns=("Firstname", "Lastname", "Address"), height=400, selectmode="extended", yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
scrollbary.config(command=tree.yview)
scrollbary.pack(side=RIGHT, fill=Y)
scrollbarx.config(command=tree.xview)
scrollbarx.pack(side=BOTTOM, fill=X)
tree.heading('Firstname', text="Firstname", anchor=W)
tree.heading('Lastname', text="Lastname", anchor=W)
tree.heading('Address', text="Address", anchor=W)
tree.column('#0', stretch=NO, minwidth=0, width=0)
tree.column('#1', stretch=NO, minwidth=0, width=200)
tree.column('#2', stretch=NO, minwidth=0, width=200)
tree.column('#3', stretch=NO, minwidth=0, width=300)
tree.pack()

In this module which is the design of the project CSV File In Python.

The code given below is for reading the CSV file.

with open('test.csv') as f:
    reader = csv.DictReader(f, delimiter=',')
    for row in reader:
        firstname = row['firstname']
        lastname = row['lastname']
        address = row['address']
        tree.insert("", 0, values=(firstname, lastname, address))

In this module which is reading the CSV file and displayed in the tkinter table.

Complete Source Code

from tkinter import *
import tkinter.ttk as ttk
import csv

root = Tk()
root.title("Python - Import CSV File To Tkinter Table")
width = 500
height = 400
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width/2) - (width/2)
y = (screen_height/2) - (height/2)
root.geometry("%dx%d+%d+%d" % (width, height, x, y))
root.resizable(0, 0)


TableMargin = Frame(root, width=500)
TableMargin.pack(side=TOP)
scrollbarx = Scrollbar(TableMargin, orient=HORIZONTAL)
scrollbary = Scrollbar(TableMargin, orient=VERTICAL)
tree = ttk.Treeview(TableMargin, columns=("Firstname", "Lastname", "Address"), height=400, selectmode="extended", yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
scrollbary.config(command=tree.yview)
scrollbary.pack(side=RIGHT, fill=Y)
scrollbarx.config(command=tree.xview)
scrollbarx.pack(side=BOTTOM, fill=X)
tree.heading('Firstname', text="Firstname", anchor=W)
tree.heading('Lastname', text="Lastname", anchor=W)
tree.heading('Address', text="Address", anchor=W)
tree.column('#0', stretch=NO, minwidth=0, width=0)
tree.column('#1', stretch=NO, minwidth=0, width=200)
tree.column('#2', stretch=NO, minwidth=0, width=200)
tree.column('#3', stretch=NO, minwidth=0, width=300)
tree.pack()

with open('test.csv') as f:
    reader = csv.DictReader(f, delimiter=',')
    for row in reader:
        firstname = row['firstname']
        lastname = row['lastname']
        address = row['address']
        tree.insert("", 0, values=(firstname, lastname, address))

#============================INITIALIZATION==============================
if __name__ == '__main__':
    root.mainloop()

Output:

How To Read CSV File in Python Output
How To Read CSV File in Python Output

Downloadable Source Code

I have here the list of Best Python Project with Source code free to download for free, I hope this can help you a lot.

Summary

This tutorial about How To Read Multiple Columns From CSV File in Python is designed in Python programming language. Python is very smooth to research the syntax emphasizes readability and it is able to reduce time-ingesting in developing

Also, this tutorial is the simplest way for beginners or students to Writing CSV File In Python.

The project contains the Python script (index.py) which is the main and the only module of this project. It also includes the given CSV File that has data inside that named(test.csv).

Inquiries

If you have any questions or suggestions about How To Read CSV File In Python With Source Code, please feel free to leave a comment below.

Leave a Comment