[GIS] Python Error: “ValueError: need more than 1 value to unpack”

arcpyarraycursorvalueerror

Can someone help me figure out what is not working in this piece of code:

import arcpy
import fileinput
import os
import string

from arcpy import env

env.workspace = "C:/EsriPress/Python/Data/Exercise08"
env.overwriteOutput = True
outpath = "C:/EsriPress/Python/Data/Exercise08"
newfc = "Results/newpolyline.shp"
arcpy.CreateFeatureclass_management(outpath, newfc, "Polyline")

infile = "C:/EsriPress/Python/Data/Exercise08/coordinates.txt"

cursor = arcpy.da.InsertCursor(newfc, ["SHAPE@"])
array = arcpy.Array()

for line in fileinput.input(infile):
    ID, X, Y = string.split(line, " ")
    array.add(arcpy.Point(X,Y))
cursor.insertRow([arcpy.Polyline(array)])
fileinput.close()
del cursor

I looked up some other questions/responses related to this error and they mention that perhaps the text file being read is not formatted correctly. Each line in my text file has an object ID, and an X & Y coordinates, with only one space in between each field (also, there is no extra line or space after the final object).

I'm wondering what else can be giving me this error.

Best Answer

With a minimum of modification to your code:

array = arcpy.Array()

with arcpy.da.InsertCursor(newfc, "SHAPE@") as cursor:
    with open(infile,'r') as inHand:
        for line in inHand:
                ID, X, Y = line.split(" ")
                Xco = float(X)
                Yco = float(Y)
                array.add(arcpy.Point(Xco,Yco))

    cursor.insertRow([arcpy.Polyline(array)])

Works for me. There could be a lock, something wrong with your text file or your implementation of fileinput, but reading the docs it seems that your implementation is correct therefore it seems the likely culprit is your text file or a persistent lock on the shapefile.

To release an existing lock, evidenced by a lock file (sr.lock, wr.lock etc) present with the shapefile, close all Esri products and hopefully it will disappear, if not, you might need to restart your computer (being that severe would be a rare event).

To try to find garbage in the text file try printing the line (add print(line) in your code on the line right after for line in..) so that when it crashes out you know which line caused the error and try to guess why.

Normally I'd put in a try: except: block on the next line after for line in inHand: because I don't trust text files to have 3 (or more) elements, the latter two being numeric, all the time regardless of how sure the creator of the file is - I've been burnt too may times by garbage in a text file to be complacent about it:

with arcpy.da.InsertCursor(newfc, "SHAPE@") as cursor:
    with open(infile,'r') as inHand:
        for line in inHand:
            LineAllGood = True
            try:
                ID, X, Y = line.split(" ")
            except:
                LineAllGood = False # don't do the next step.
                arcpy.AddWarning('Line {} produces an error in splitting'.format(line))

            if LineAllGood: # The splitting worked, now try to float both
                try:
                    Xco = float(X)
                    Yco = float(Y)
                    array.add(arcpy.Point(Xco,Yco))
                except:
                    arcpy.AddWarning('Conversion error X {} or Y {} not a float'.format(X,Y))

    cursor.insertRow([arcpy.Polyline(array)])
Related Question