代码之家  ›  专栏  ›  技术社区  ›  Rio

CSV分析特定行错误[8]:列表索引超出范围

  •  0
  • Rio  · 技术社区  · 7 年前

    我正在尝试从CSV文件中获取行值,除了 row[8] . 但同一行和 row[7] 如果我删除它前面的任何其他行。

    comment = row[8]
    IndexError: list index out of range
    

    190,3391d5ae,,Host-1,FuelNode-1,"http,tcp-8000",False,,created on - 6/10/14
    192,25dbbf4c,sam,"vlan85,vlan86","Srv1,Srv2",dhcp-relay,False,25-10-2018,
    

    请注意,很少有字段没有值或为空。

    这是我写的代码。

    outFiles = os.listdir('datastore/csv_out_files')
    ext = "*.csv"  
    for csvoutfile in outFiles:
      if fnmatch.fnmatch(csvoutfile, ext):
         with open('datastore/csv_out_files/'+ csvoutfile) as f:
            rows = csv.reader(f, delimiter=',')
            headers = next(rows)
            for row in rows:
                uid = row[1]
                name = row[2]
                source = row[3]
                destination = row[4]
                service = row[5]
                enabled = row[6]
                use_date = row[7]
                comment = row[8]
    
                if not use_date:
                    use_date = "Never Used"
    
                print "              UID:", (uid)
                print "             Name:", (name)
                print "           Source:", (source)
                print "      Destination:", (destination)
                print "          Service:", (service)
                print "          Enabled:", (enabled)
                print "        Last Used:", (use_date)
                print "  Additional Info:", (comment)
    

    请帮帮我,我想不出来。

    0 回复  |  直到 7 年前