Reading data from a CSV file in Python

后端 未结 8 618
你的背包
你的背包 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 16:51

    Your first line only has one column, so the process fails and doesn't continue. To solve, just skip first row

    >>> with open( path, "r") as file:
    ...     reader = csv.reader(file)
    ...     for idx,line in enumerate(reader):
    ...         if idx>0:
    ...             t=line[1],line[2]
    ...             print t
    ... 
    ('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 ')
    

提交回复
热议问题