Python - importing .csv file and list the content

Dear Experts,

I am trying to construct a small code in Python (through some Google searching) with the following data,
Example file
which is able to import them in a .csv file from the computer and read it into a csv iterator, which then looped through to arrays data_header and data_rows:

f = open(r"C:\Demo.csv")

data_ob = csv.reader(
data_header = []
data_rows = []

for v in data_ob:
    if not data_header:
        data_header = v
        v = [float(v[0]),

Open in new window

As I understand the data_header and data_rows arrays should contain already the imported data which maybe could be listed with the following syntax:
for x in data_header:
for x in data_rows:

Open in new window

but that does not list anything, could you please advise?

Thanks in advance,
csehzIT consultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The csv reader requires to pass the file object opened in text mode with special arguments. Follow the documentation 14.1. csv — CSV File Reading and Writing -- no read, no splitlines.

My experience also is to give the helper object the same name for the purpose. So, I would write the code like this (from head, not tested):

import csv

fname = r'C:\Demo.csv'

with open(fname, 'r', encoding='utf-8', newline='') as f:
    reader = csv.reader(f)
    data_header = None
    data_rows = []

    for row in reader:
        if not data_header:
            data_header = row
            row = [float(row[0]),  float(row[1]), float(row[2]), float(row[3]), str(row[4])]

Open in new window

csehzIT consultantAuthor Commented:
Thank you for the answer, trying your code the following error message:

NameError: name 'v' is not defined

Could you please advise what does it mean?
Since you're just isolating the header row from the rest, try this simpler code:
import csv

with open(r"C:\Demo.csv") as csvfile:
    rows = list(csv.reader(csvfile))
    data_header = rows[0]
    data_rows = rows[1:]


Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

csehzIT consultantAuthor Commented:
@Aikimark thanks a lot, that simplier code is absolutely enough for my scenario
You're welcome.  Did you need to do the float casting or just isolate the header from the data?
csehzIT consultantAuthor Commented:
Running your code in that format it perfectly fits to my targets with isolating the header from the data, based on that I would like to create charts, probably will ask on EE further too :-)
To add, when the CSV is huge, you should not read it to the list (that is to the memory).

In any case, you should add the newline='' to the open. Maybe, it is not necessary in this particular case, but generally, it is a bug.
csehzIT consultantAuthor Commented:
@pepr thanks for the advice, actually in this project there are relatively smaller amount of data to show on charts so presumably will be not neccessary
OK. But I suggest to get used to newline=''. The reason is that the csv module wants to treat the newlines on its own. The reason behind it is that quoted values inside a CSV file may contain newlines sometimes (in general). This is the reason why Python 2 required to open the file in binary mode. This is not possible in Python 3, because all strings in Python 3 are unicode strings. And you get them from a file. And to do that, you have to tell the file content encoding (implicitly UTF-8), and to do that the file have to be open in text mode.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.