Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

csv writer in python adding double quote when copying data from another csv file, So I just need to know how to copy exactly the same data

#Reading from csv file
with open('py_csv/oscar_age_male.csv','r') as csv_file:
    csv_reader=csv.reader(csv_file)

    #Creating new file and opening it and changing delimiter
    with open('py_csv/newfile.csv','w')as newfile:
        csv_writer=csv.writer(newfile)

#Writing Row by row
        for line in csv_reader:
            csv_writer.writerow(line)

Data from originals file=

1, 1928, 44, "Emil Janning", "The Last Command, The Way of All Flesh"
2, 1929, 41, "Warner Baxter", "In Old Arizona"

Data after being copied=

1, 1928, 44," ""Emil Janning"""," ""The Last Command"," The Way of All Flesh"""
2, 1929, 41," ""Warner Baxter"""," ""In Old Arizona"""

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The problem is the spaces after the commas. Quotes are allowed around a field in a CSV, but since the field starts with a space, the quotes don’t appear to be surrounding the whole field value. So they’re being treated as literal quotes that need to be doubled in the output.

Use the skipinitialspace option to ignore those spaces.

csv_reader=csv.reader(csv_file, skipinitialspace=True)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading