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

Question:

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:CloudsOneDriveDesktoppython_tamrinstudents.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') 
Asked By: nasrin

||

Answers:

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])
Answered By: Guy
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.