Reading data from a CSV file in Python

后端 未结 8 602
你的背包
你的背包 2020-11-28 16:36

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-         


        
相关标签:
8条回答
  • 2020-11-28 17:14

    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)
    
    0 讨论(0)
  • 2020-11-28 17:14
    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 
    
    0 讨论(0)
提交回复
热议问题