Python - writing to a .csv file

csehz
csehz used Ask the Experts™
on
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:
    print(x)
    
for x in data_rows:
    print(x)

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,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
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)
print(df)

df.to_csv(file,index= False)

Open in new window

csehzIT consultant

Author

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 consultant

Author

Commented:
Do you have maybe an idea which does not use pandas library, so rather the import csv one?
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
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.
#!python3

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)
    
    print(rows)
    
    rows[1][0] = '10108'        # change the element
    
    writer = csv.writer(fout, dialect='excel', delimiter=';')
    for row in rows:
        writer.writerow(row)

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.
csehzIT consultant

Author

Commented:
@pepr thank you that works great

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial