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(f.read().splitlines())
data_header = []
data_rows = []

for v in data_ob:
    if not data_header:
        data_header = v
    else:
        v = [float(v[0]),
             float(v[1]),
             float(v[2]),
             float(v[3]),
             str(v[4])]
        data_rows.append(v)

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:
    print(x)
    
for x in data_rows:
    print(x)

Open in new window


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

Thanks in advance,
LVL 1
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.

peprCommented:
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):
#!python3

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
        else:
            row = [float(row[0]),  float(row[1]), float(row[2]), float(row[3]), str(row[4])]
        data_rows.append(row)

Open in new window

0
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?
0
aikimarkCommented:
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:]

    #print(len(data_header))
    #print(data_header)
    #print(len(data_rows))
    #print(data_rows[0])

Open in new window

0

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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

csehzIT consultantAuthor Commented:
@Aikimark thanks a lot, that simplier code is absolutely enough for my scenario
0
aikimarkCommented:
You're welcome.  Did you need to do the float casting or just isolate the header from the data?
1
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 :-)
0
peprCommented:
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.
0
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
1
peprCommented:
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.
0
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
Python

From novice to tech pro — start learning today.