From Csv Import Reader

Hello Guys,

I really want to grab the full concept of us always using

from “csv import reader” in the guilded one project.

please whats the essence of using such statement in python?

i’m new to python and programming as a whole. wish someone could help me out on this

1 Like

When working over the source data, you can always write your own code to open file, load data and pre-process them, i.e. read line by line, separate with selected delimiters, etc., etc., etc. But why reinvent the wheel, if these typical tasks are already solved and saved in the form of the modules for Python?

So in order to use standard functionality, you need to import it into your code, so that it can be used.
Therefore, to read and preprocess csv files we either import the whole csv module, which contains a lot of standard functions for working with csv files (read, write, etc) or we may want to only import one function from that module, for instance, only reader.

Hence goes this line:
from csv import reader

You can always read official doc to see what’s inside and learn more. For instance, official docs for csv.reader is here:

And here is more about importing modules:

Hope this helps.


Thank you a lot, i appreciate

Later on there will be a mission that elaborate clearly on various ways to import modules into Python and explain more clearly, so don’t worry about that for now. Cheers!


Using DictReader class

Python’s DictReader class basically creates a CSV object that behaves like a Python OrderedDict . It works by reading in the first line of the CSV and using each comma separated value in this line as a dictionary key. The columns in each subsequent row then behave like dictionary values and can be accessed with the appropriate key.

I just started the first project and I’m also struggling to understand the relationship between reader() function and csv (is it called a mode?). Thanks for the explanation @ranklord.
I also found that there are a few ways to call reader()
I’ll give examples from the first project (I’m sure there are other variations besides these two):
from csv import reader | opened_file = open('AppleStore.csv') | read_file = reader(opened_file)

import csv | opened_file = open('AppleStore.csv') | read_file = csv.reader(open_file_a)