I am trying to load data from a csv file (that contains well_id, x, y, elevation) to a shapefile. In my script, it adds fields from the csv field header that is not in the shapefile. It then updates existing well_id, and add well_id that does not exist.

Here is what my code looks like:

import csv,arcpy,codecs,ast 
csvfile = r'\\Cd1001-f18\gis\Clients\Enbridge\Line4_MP66_75\Project_Data\Table\mw_bh_loc_20171115.csv'

fc = r'Q:\Clients\Enbridge\Line4_MP66_75\Project_Data\bh_mw_loc.shp'

with codecs.open(csvfile,'rb',encoding="utf-8-sig") as csvFile:

    csvheader1 = [x.strip() for x in next(csvFile).split(',')]
    print csvheader1
    for row in csv.reader(csvFile):
        print row

gdbFields = arcpy.ListFields(fc)
insertList = []

for x in range (0,len(csvheader1)):
csvheader2 = list(csvheader1)
insertList2 = tuple(csvheader2)

print csvheader2

newFields = [x for x in csvheader1 if x not in gdbFields]
print newFields
for x in newFields:
gdbNewFields = gdbFields + newFields
insertFields = csvheader1.append("SHAPE@XY")
fcWellIDValues = [row[0] for row in arcpy.da.SearchCursor(fc, "Well_ID")]
print fcWellIDValues
csvDict = csv.DictReader(codecs.open(csvfile, encoding ="utf-8-sig"))
for csvrow in csvDict:

    if csvrow["Well_ID"] in fcWellIDValues:
        with arcpy.da.UpdateCursor(fc, gdbNewFields) as cursor:
            for row in cursor:
                if row["Well_ID"] == csv["Well_ID"]:
                    for x in newFields:
                        row[x] == csvrow[x]

    elif csvrow["Well_ID"] not in fcWellIDValues:
        with arcpy.da.InsertCursor(fc,csvheader2)as cursor:

print "done"

I am getting an error on the insertcursor.

Traceback (most recent call last):
  File "C:\Users\vlaw\Desktop\Application\csv_mwloader.py", line 47, in <module>
TypeError: sequence size must match size of the row

closed as off-topic by PolyGeo Feb 15 '18 at 0:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    This seems to be your whole code rather than a code snippet. Try printing your two variables csvheader2 and insertList2. If they are not lists of the same length then that will explain the error message. – PolyGeo Feb 15 '18 at 0:02
  • 1
    You seem to be using arcpy.da.InsertCursor with arcpy.InsertCursor syntax. DataAcess is list-oriented, not dictionary-fueled. – Vince Feb 15 '18 at 11:48
  • Thanks for your insight @Vince I really appreciate it! I will fix up my syntax and see how it goes :D – Vincent Law Feb 15 '18 at 15:15

Browse other questions tagged or ask your own question.