Sunday, 24 April 2016

arcpy - Error adding row - sequence size must match size of row


I have this table with one row


timestamp_pretty   3.1.2014 9:13
timestamp 1,38874E+12
msgid 3
targetType A
mmsi 205533000
lat 53.4346
long 14.580546
posacc 0

sog 0
cog 319.5
shipType CARGO
dimBow 68
draught 4
dimPort 6
dimStarboard 5
dimStern 12
month 1
week 1

imo 8404446
country Belgium
name FAST JULIA

I want to make a point feature class from arcpy using insert cursor:


# Read the csv
csv.register_dialect("xls", lineterminator="\n")
f = open(incsv, "r")
reader = csv.reader(f, dialect = "xls")


# Add fields to fc
desc = arcpy.Describe(incsv)
for field in desc.fields:
arcpy.AddField_management(outfc,field.name,field.type)

# the fieldnames
fields = desc.fields
fieldnames = [field.name for field in fields]

# Create InsertCursor.

cursor = arcpy.da.InsertCursor(outfc, ['SHAPE@XY'] + fieldnames)
count = 0
next(reader, None) # skip the header
for row in reader:
if count % 10000 == 0:
print "processing row {0}".format(count) + " of " + table
Ycoord = row[5]
Xcoord = row[6]
newrow = [(float(Xcoord), float(Ycoord))] + row[0:]
cursor.insertRow([newrow])

count += 1
del cursor
f.close()

But I get this error:


line 130, in 
cursor.insertRow([newrow])
TypeError: sequence size must match size of the row

I've been through SE similar answers and made many tests (days) but to no avail.



****EDIT****


If I print the result of newrow and row[0:] like this:


newrow = [(float(Xcoord), float(Ycoord))] + row[0:]
print "new row: "+str(newrow)
print "row[0:]: "+str(row[0:])

*EDIT 2 * name and type use for create feature class


[u'timestamp_pretty', u'timestamp', u'msgid', u'targetType', u'mmsi', u'lat', u'long', u'lat_D', u'long_D', u'posacc', u'sog', u'cog', u'shipType', u'dimBow', u'draught', u'dimPort', u'dimStarboard', u'dimStern', u'month', u'week', u'imo', u'country', u'name']
[u'Date', u'Double', u'Integer', u'String', u'Integer', u'String', u'String', u'Double', u'Double', u'Integer', u'String', u'String', u'String', u'Integer', u'String', u'Integer', u'Integer', u'Integer', u'Integer', u'Integer', u'Integer', u'String', u'String']


I get this result:


new row: [(14.580546, 53.4346), '03/01/2014 09:13:26', '1388740406000', '3', 'A', '205533000', '53.4346', '14.580546', '0', '0', '319.5', 'CARGO', '68', '4', '6', '5', '12', '01', '01', '8404446', 'Belgium', 'FAST JULIA']
row[0:]: ['03/01/2014 09:13:26', '1388740406000', '3', 'A', '205533000', '53.4346', '14.580546', '0', '0', '319.5', 'CARGO', '68', '4', '6', '5', '12', '01', '01', '8404446', 'Belgium', 'FAST JULIA']

I now, newrow has 22 fields (counting the coordinates in the beginning) and row[0:] has 21. Is that the error? If so why did it work in the original script I got from @John?




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...