Python - writing to a .csv file

Dear Experts,

I have a small Excel file with the data as it the below print screen shows:
Excel file
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:
for x in data_rows:

Open in new window

Could you please advise which Python syntax could write back data to that Excel file to cell A2 (so to the place of value '10101')?

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.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
What are you trying to achieve here by reading the file and writing back to the same file?

BTW you may give this a try...
import pandas as pd

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

df.to_csv(file,index= False)

Open in new window

csehzIT consultantAuthor Commented:
Neeraj thanks I will try, in the meantime confirming that yes, I would like to have this input file in Python and writing back to it also some values (those will come from other application as final target)
csehzIT consultantAuthor Commented:
Do you have maybe an idea which does not use pandas library, so rather the import csv one?
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
There are multiple libraries you can use to deal with csv files but I find pandas very much practical when dealing with tabular data, it's my personal preference. Actually I don't use Python much so have no idea about csv library and it's methods. You may look into the csv documentation for more info.
Here the code uses the rows to make it more apparent. However, it can easily be modified for working with data_header and data_rows. The data_header would simply be the first row to be written by writer and then the data_rows would be looped.

import csv

fname_in = 'File.csv'
fname_out = 'File_out.csv'

with open(fname_in, newline='') as fin, \
     open(fname_out, 'w', newline='') as fout:
    reader = csv.reader(fin, dialect='excel', delimiter=';')
    rows = list(reader)
    rows[1][0] = '10108'        # change the element
    writer = csv.writer(fout, dialect='excel', delimiter=';')
    for row in rows:

Open in new window

I have separated the output to the other file (for experiments). The print(rows) just shows the representation of what was read.

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
csehzIT consultantAuthor Commented:
@pepr thank you that works great
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.