Python - import .csv file and get a cell value from it

Dear Experts,

I have a small Excel file with the data as it the below print screen shows:

Excel data
which are imported by Python through a .csv file by the following code:
import csv

with open(r"C:\Users\ac324485\Downloads\Daily files\File.csv") as csvfile:
    rows = list(csv.reader(csvfile))
    data_header = rows[0]
    data_rows = rows[1:]
    
for x in data_header:
    print(x)
    
for x in data_rows:
    print(x)

Open in new window


and which can be seen as result (the first row as header in data_header array, while the next of the rows as data line in data_rows array:

Imported .csv data
Could you please advise how to refer/get the value for '10101' in cell A2?

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:
data_rows[0][0]

Open in new window

The first index is for the row, the second for the element.
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
zvitamConsultantCommented:
You can have it all by using pandas instead of csv

import pandas as pd

df = pd.read_csv("C:\Users\ac324485\Downloads\Daily files\File.csv")

print( df )

Now you have all the data insdide the dataframe.
1
peprCommented:
By the way, you should get used to open the file like this open(r'C:\Users\ac324485\Downloads\Daily files\File.csv', newline='') as mentioned in the csv documentation. The reason is that sometimes (the more complex) CSV files may contain newlines at "unexpected" places and the csv module wants to decide whether they are part of a value or whether they really are the CSV-file line delimiters.
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

csehzIT consultantAuthor Commented:
Thanks for the answers, both are working.

@zvitam, especially thanks for that pandas method as it imports very clearly,

Pandas import
in that case which syntax would refer to '10101'?
0
zvitamConsultantCommented:
df.iloc[0][0] should return the right value
0
csehzIT consultantAuthor Commented:
Thank you just again, learnt from both ways
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Correct way of using pandas iloc method is...
df.iloc[row_index, column_index]
i.e. to get the value of the first cell in first row and first column, try this...
df.iloc[0,0]

In pandas DataFrame, the row_index and column_index start with 0.
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.