pythonexport-csv

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

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])