0

I am writing a script that will export feature class metadata to a GDB table. The metadata is first obtained through arcpy.Describe and by parsing an exported XML file. The script then creates a GDB table, adds fields, and uses arcpy.da.UpdateCursor to populate the fields. The script runs and entering the various variables in the PythonWin interactive window returns the correct values, but they are not writing to the GDB table, so I end up with a table that has all the correct fields but no information. What is wrong with my UpdateCursor?

Code:

import arcpy
import csv
from xml.etree.ElementTree import ElementTree
from time import strftime
arcpy.env.overwriteOutput = True
# Set Translator folder path
AGSHOME = arcpy.GetInstallInfo("Desktop")["InstallDir"]
xslt = AGSHOME + r"Metadata\Stylesheets\gpTools\exact copy of.xslt"
# Get date and time
dateTime = strftime('%d%b%Y_%H%M')
# Parameters
data = r'T:\FS\Reference\GeoTool\r06\DatabaseConnection\r06_csa2_default_as_myself.sde\S_R06_CSA2.PortOrfordCedar\S_R06_CSA2.PortOrfordCedar_2013'
workspace = r"T:\FS\NFS\R06\Program\ResourceInfoMgmt\GIS\CSA2\Workspace\astratton\R6\SDE_Metadata_Python\Test"
# Get name for new folder in Workspace
descData = arcpy.Describe(data)
outputName = descData.name.replace('.', '_') + dateTime
# Create folder in Workspace
folder = arcpy.CreateFolder_management(workspace, outputName)
desc_fold = arcpy.Describe(folder)
fold_path = desc_fold.path
# Create GDB
table_GDB = arcpy.CreateFileGDB_management(folder, "Tables")
desc = arcpy.Describe(data)
path = str(desc.path)
file = str(desc.file)
print "Working on " + file
dataType = str(desc.dataType)
if desc.dataType != "Table":
    sr = desc.spatialReference
    sr_name = str(sr.name)
else:
    sr = "None"
# Export metadata to xml
outXML = "Temp_Metadata"
outFileXML = os.path.join(fold_path, ("{0}.xml".format(outXML)))
arcpy.XSLTransform_conversion(data, xslt, outFileXML)
tree = ElementTree()
tree.parse(outFileXML)
# Check for presence Metadata 10
spot = tree.find("dataIdInfo/idCitation/date/pubDate")          # Meta 10
if spot != None:
    Meta_10 = "Yes"
else:
    Meta_10 = "No"
# Check for presence Metadata 9
spot = tree.find("idinfo/citation/citeinfo/pubdate")            # Meta 9
if spot != None:
    Meta_9 = "Yes"
else:
    Meta_9 = "No"
# Get title (10 and 9)
spot = tree.find("dataIdInfo/idCitation/resTitle")              # Title
if spot != None:
    title = spot.text
else:
    title = ""
spot = tree.find("idinfo/citation/citeinfo/title")
if spot != None:
    title = spot.text
else:
    pass
# Get publication date (10 and 9)
spot = tree.find("dataIdInfo/idCitation/date/pubDate")          # Publication date
if spot != None:
    pub_date = spot.text
else:
    pub_date = ""
spot = tree.find("idinfo/citation/citeinfo/pubdate")
if spot != None:
    pub_date = spot.text
else:
    pass
table = arcpy.CreateTable_management(table_GDB, file.replace(".", "_"))
fields = ["Path", "FileName", "Data_Type", "sr", "Meta_10", "Meta_9", "Title","Pub_date"]
for field in fields:
    arcpy.AddField_management(table, field, "TEXT")
with arcpy.da.UpdateCursor(table, fields) as cursor:
    for row in cursor:
        row[0] = path
        row[1] = file
        row[2] = dataType
        row[3] = sr_name
        row[4] = Meta_10
        row[5] = Meta_9
        row[6] = title
        row[7] = pub_date
        cursor.updateRow(row)

del cursor
print "Done."
1
  • 3
    Are your metadata items being populated from the tree? What do you get if you print them out? Note you don't need to del cursor, the with block handles that. Commented Apr 21, 2017 at 1:16

1 Answer 1

2

An UpdateCursor updates, which may sound redundant, but it means that it operates on existing data. If you were running through an existing table and changing a field's value, you would use an UpdateCursor.

Maybe I am misunderstanding your code, but you create the table a few lines above the UpdateCursor. It would be empty at that time. That UpdateCursor call will return no rows in your cursor.

Look into InsertCursor which is meant to create data; it behaves much like the UpdateCursor.

From your question, I don't see where the rows of data are. You'll need a list of tuples in the order of the fields to loop through and insert into the table. From the help documentation:

import arcpy

# A list of values that will be used to construct new rows
row_values = [('Anderson', (1409934.4442000017, 1076766.8192000017)),
              ('Andrews', (752000.2489000037, 1128929.8114))]

# Open an InsertCursor
cursor = arcpy.da.InsertCursor('C:/data/texas.gdb/counties',
                               ['NAME', 'SHAPE@XY'])

# Insert new rows that include the county name and a x,y coordinate
#  pair that represents the county center
for row in row_values:
    cursor.insertRow(row)

# Delete cursor object
del cursor

If you are just trying to insert that 1 row, try:

with arcpy.da.InsertCursor(table, fields) as cursor:
    cursor.insertRow((path, file, dataType, sr_name, Meta_10, Meta_9, title, pub_date))
1
  • This answer was very helpful and in fact made the process shorter since I originally planned on making multiple tables and merging them at the end of the process. Now I can create the table and add the lines as needed by iterating though an SDE, so excellent answer this solved my problem.
    – user89051
    Commented Apr 21, 2017 at 17:12

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.