Runtime error <type 'exceptions.IndexError'>: list index out of range

1515
6
06-05-2012 07:36 AM
AnthonyTimpson2
Occasional Contributor
my List indices are 0 and 1 those are the only two..

Can anyone help spot what's causing that?


import arcpy
from arcpy import env


arcpy.env.overwriteOutput = True

#set working MDB Here

arcpy.env.workspace = "C:/Users/atimpson/Desktop/Brunswick/PowerPlant.mdb/BPDMLayers"
dataList = arcpy.ListFeatureClasses()

mxd = arcpy.mapping.MapDocument("CURRENT")
lyrlist = arcpy.mapping.ListLayers(mxd)

# Reads a base table and outputs Feature Class and Layer Lists

readTable = open("C:/Users/atimpson/Desktop/ScriptTable/BPWorksLayers.csv")

# Figure out position of feature class and layer in the header
headerLine = readTable.readline()
valueList = headerLine.split(",")

fcPos = valueList.index("Feature Class")
lyrPos = valueList.index("Layers")

# Read lines in the file and append to comparison lists
baseFeature = []
baseLayer = []

for line in readTable.readlines():
 segmentedLine = line.split(",")
 baseFeature.append([segmentedLine[fcPos]])
 baseLayer.append([segmentedLine[lyrPos]])

#Performs comparisons between project data/layers and idealized data/layers

if "BPWorks 2 Layers" in lyr.name:     

 for lyr, base in zip(lyrList, baseLayer):
  if lyr != base:
   print(lyr, "is different from", base)
Tags (2)
0 Kudos
6 Replies
JohnCobb1
New Contributor
Are you running this within the Python Window? The 'current' keyword might be giving you trouble, I don't think you can call it from a script.

mxd = arcpy.mapping.MapDocument("CURRENT")
0 Kudos
AnthonyTimpson2
Occasional Contributor
Running from within Arcmap

i can individually call up the variables.
0 Kudos
markdenil
Occasional Contributor III
Try testing the length of segmentedLine. Maybe it is not always what you expect.
0 Kudos
PhilMorefield
Occasional Contributor III
Not sure what's causing the problem exactly. But a couple of things could be cleaned up, and that might straighten out weird behavior. I most often get this same error when one of my lists unexpectedly contains zero items.

First, I've always seen/used the mode type declared when creating a file object:
readTable = open("C:/Users/atimpson/Desktop/ScriptTable/BPWorksLayers.csv", "r")  # 'r' is for 'read only'

Second, you've got some extra brackets, which means you're actually appending a list to a list. I'm guessing that's not what you intended to do:
for line in readTable.readlines():
 segmentedLine = line.split(",")
 baseFeature.append(segmentedLine[fcPos])  # I removed an extra bracket here
 baseLayer.append(segmentedLine[lyrPos])  # and here

And finally, troubleshooting is a lot easier when you have line number and traceback information:
# I wrap this around all of my scripts, pretty much.

import traceback
import sys

try:
    # all of your code here
except:
    tbinfo = traceback.format_tb(sys.exc_info()[2])
    print "Traceback Info:\n"
    for item in tbinfo:
        print item + "\n"
    print "Error Info:\n{0}: {1}\n".format(sys.exc_type, sys.exc_value)
0 Kudos
NobbirAhmed
Esri Regular Contributor
Use Python csv module - it will make your life easier - see below 🙂

import csv
# Reads a base table and outputs Feature Class and Layer Lists
readTable = csv.reader(open("C:/Users/atimpson/Desktop/ScriptTable/BPWorksLayers.csv", "rb")

# Read the first line
first_line = readTable.next()

## comment out these two lines
#headerLine = readTable.readline()
#valueList = headerLine.split(",")

fcPos = first_line.index("Feature Class")
lyrPos = first_line.index("Layers")

# Read lines in the file and append to comparison lists
baseFeature = []
baseLayer = []

# now read second line and on ..
for line in readTable:

    # make sure current line has as many items as the first line has
    if len(line) >= len(first_line):
        baseFeature.append(line[fcPos])
        baseLayer.append(line[lyrPos])
0 Kudos
AnthonyTimpson2
Occasional Contributor
thank you everyone. I will try your suggestions this afternoon!

Really appreciate the help
0 Kudos