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."