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

export part of CSV file to a new CSV file with Python

i need to export specific column from csv to new csv, but i couldn’t get proper result.plase support to chcek

Id Name Course City Session

21 Mark Python London Morning

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

22 John Python Tokyo Evening

32 Shaun Java Tokyo Morning

import csv
with open(r'D:\Clouds\OneDrive\Desktop\python_tamrin\students.csv', 'r') as read_obj:
    csv_reader = csv.reader(read_obj)
    with open ('new_nemes_1.csv','w', newline='') as new_file:
        csv_writer=csv.writer(new_file)
        for line in csv_reader:
            c_1 =row['Id']
            csv_writer.writerow('c_1') 

>Solution :

line (or row) is a list, it doesn’t have Id key. You need to write a slice of that list to the new file

...
for row in csv_reader:
    csv_writer.writerow(row[:1])
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