0

I'm trying to update fields with updateCursor, but it doesn't work at all. I've worked with updateCursor before many times but this time - no success.

I have a feature class (frameBoundaryBeforeAT) in which I need to update his fields according to a text file. I extracted the name and other parameters from the text and append them into a list.

I'm going through the list and i compare between the name in the fc and the name in the list= (if str(row[10]) == info_extract[0]) and if it's the same name, I want to update the other fields in fc. The loop is fine, it matches the names, the if is working (I've checked it with a simple print - print "it's a match") but when it's come to updating the field - nothing happened.

If I delete the if and just update the fields with some random characters it works, but the if statement is ok so what is the problem?

I tried to print after the line: row[0] = info_extract[1], just to see what it does and I can see the name that I wanted, but when I open the fc I see empty fields.

db = r"D:\Work work work\A\arc\per_day_GDB\Flight_020721.gdb"
txt = r"D:\Work work work\A\arc\Data_for_GDB\Best_fit_eop\noa.txt"

txt_list = extract_list_from_txt(txt)
frameBoundaryBeforeAT = db + "//frameBoundaryBeforeAT"
fieldnames = ["Frame","Camera","Easting","Northing","MSL_Height","Omega","Phi","Kappa","Date","Time","Feature_Name"]

for line in txt_list:
    info_extract = extract_info_from_list(line)
    with arcpy.da.UpdateCursor(frameBoundaryBeforeAT, fieldnames) as cursor:
         for row in cursor:
            if str(row[10]) == info_extract[0]:
                print "it's a match"
                row[0] = info_extract[1]
                row[1] = "DMCIIe_250"
                row[2] = info_extract[2]
                row[3] = info_extract[3]
                row[4] = info_extract[4]
                row[5] = info_extract[5]
                row[6] = info_extract[6]
                row[7] = info_extract[7]
                #Urow[8] = info_extract[8]
                row[9] = info_extract[9]
                cursor.updateRow(row)
1
  • 4
    You've structured this code incorrectly. Use the text file to build a dictionary, then make one update pass with the cursor.
    – Vince
    Commented Nov 28, 2021 at 14:52

1 Answer 1

0

Your code is highly inefficient because you're iterating over the entire shapefile for each line of your text file ! Maybe this is causing some issues.

As Vince said in his comment, create a dict with your txt file information and then iterate only one time with the cursor :

db = r"D:\Work work work\A\arc\per_day_GDB\Flight_020721.gdb"
txt = r"D:\Work work work\A\arc\Data_for_GDB\Best_fit_eop\noa.txt"

txt_list = extract_list_from_txt(txt)
frameBoundaryBeforeAT = db + "//frameBoundaryBeforeAT"
fieldnames = ["Frame","Camera","Easting","Northing","MSL_Height","Omega","Phi","Kappa",
"Date","Time","Feature_Name"]

extracted_info = {}
for line in txt_list:
    # I suppose extract_info_from_list creates a list from a line of your txt file
    extracted = extract_info_from_list(line)
    extracted_info[extracted[0]] = extracted

with arcpy.da.UpdateCursor(frameBoundaryBeforeAT, fieldnames) as cursor:
    for row in cursor:
        if str(row[10]) in extracted_info:
            print "it's a match : {}".format(row[10])
            info_extract = extracted_info[str(row[10])]
            row[0] = info_extract[1]
            row[1] = "DMCIIe_250"
            row[2] = info_extract[2]
            row[3] = info_extract[3]
            row[4] = info_extract[4]
            row[5] = info_extract[5]
            row[6] = info_extract[6]
            row[7] = info_extract[7]
            # Urow[8] = info_extract[8]
            row[9] = info_extract[9]
            cursor.updateRow(row)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.