I am reading data from a CSV file (xyz.CSV) which contains below data:
col1,col2,col3,col4
name1,empId1,241682-27638-USD-CIGGNT ,1
name2,empId2,241682-27638-
Hope it clears the issue
import csv
file=open( "xyz.CSV", "r")
reader = csv.reader(file)
for line in reader:
t=line[0]+","+line[1]
print (t)
import csv
csv_file=open("xyz.csv", "r")
reader = csv.reader(csv_file)
for row in reader:
print(" ".join(row[:2]))
Output :-
col1 col2
name1 empId1
name2 empId2
name3 empId3
name4 empId4
name5 empId5
name6 empId6
name7 empId7
name8 empId8
name9 empId9
name10 empId10
Just put value in row as slice. Below is code for printing 2nd and 3rd coloumn.
import csv
csv_file=open("xyz.csv", "r")
reader = csv.reader(csv_file)
for row in reader:
print(" ".join(row[1:3]))
output:
col2 col3
empId1 241682-27638-USD-CIGGNT
empId2 241682-27638-USD-OCGGINT
empId3 241942-37190-USD-GGDIV
empId4 241942-37190-USD-CHYOF
empId5 241942-37190-USD-EQPL
empId6 241942-37190-USD-INT
empId7 242066-15343-USD-CYJOF
empId8 242066-15343-USD-CYJOF
empId9 242066-15343-USD-CYJOF
empId10 241942-37190-USD-GGDIV