This script successfully updates fields in a table based on other fields. I've been testing on a feature class in a FGDB, but my goal is to use this script on a hosted feature layer. For that I'd imagine working in a Jupyter notebook would be the way to go?
import arcpy, os
fc = r'\Test\Test.gdb\survey'
fields = ['Email_Category', 'whats_your_question_about',
'tax_related_question', 'zoning_or_flood_zone_question']
with arcpy.da.UpdateCursor(fc, fields) as cursor:
for row in cursor:
if row[1] == 'tax_assessment':
row[0] = 'tax assessment'
if row[2] == 'tax_bill':
row[0] = 'tax bill'
if row[3] == 'flood_zone':
row[0] = 'flood zone'
cursor.updateRow(row)
print("Finished")
I have Jupyter all set up and have created an item. But, if I put my script in verbatim I get this error.
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-24-6a7e13cce2ac> in <module>
----> 1 with arcpy.da.UpdateCursor(survey, fields) as cursor:
2 for row in cursor:
3 if row[1] == 'tax_assessment':
4 row[0] = 'tax assessment'
5 if row[2] == 'tax_bill':
RuntimeError: 'in_table' is not a table or a featureclass
Solved! Go to Solution.
I ran the following successfully:
import arcpy, requests, json
# Disable warnings
requests.packages.urllib3.disable_warnings()
# Search cursor for feature service
fc = "https://services.arcgis.com/fGsbyIOAuxHnF97m/ArcGIS/rest/services/service_0a8c947fd081446eb3a31ef7ca0b966a/FeatureServer/0"
fields = ['Email_Category', 'whats_your_question_about',
'tax_related_question', 'zoning_or_flood_zone_question']
with arcpy.da.UpdateCursor(fc, fields) as cursor:
for row in cursor:
if row[1] == 'tax_assessment':
row[0] = 'tax assessment'
elif row[2] == 'tax_bill':
row[0] = 'tax bill'
elif row[3] == 'flood_zone':
row[0] = 'flood zone'
cursor.updateRow(row)
del cursor
I cannot see from your screen shot, but I'm guessing it may be the Feature Service URL. Did you append the /0 at the end?
Hey Jared,
If you're running the UpdateCursor on a hosted feature service, you will want to specify the URL to the feature service (i.e. https://portal.esri.com/arcgis/rest/services/Airports/FeatureServer/0).
Also, unless the service is shared with Everyone, whether the service is hosted in AGOL or Portal, you will need to generate a token. Below is an example on how to generate one for each.
import arcpy, requests, json
# Disable warnings
requests.packages.urllib3.disable_warnings()
# Generate token for hosted service in AGOL
username = 'admin'
password = 'gis12345'
tokenURL = 'https://www.arcgis.com/sharing/rest/generateToken'
params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'http://www.arcgis.com'}
r = requests.post(tokenURL, data = params, verify=False)
response = json.loads(r.content)
token = response['token']
# Generate token for hosted sevice in Portal
username = 'portaladmin'
password = 'gis12345'
tokenURL = 'https://portal.esri.com:7443/arcgis/sharing/rest/generateToken/'
params = {'f': 'pjson', 'username': username, 'password': password, 'referer': 'https://portal.esri.com'}
r = requests.post(tokenURL, data = params, verify=False)
response = json.loads(r.content)
token = response['token']
# Search cursor for feature service
fc = "https://portal.esri.com/arcgis/rest/services/Hosted/Airports/FeatureServer/0?token={0}".format(token)
with arcpy.da.SearchCursor(fc, ["*"]) as cursor:
for row in cursor:
print(row[0])
del cursor
with arcpy.da.SearchCursor(fc, ["*"]) as cursor:
for row in cursor:
print(row[0])
del cursor
Hi Jake,
The service is shared with everyone. I put your cursor snippet in a cell and ran it. It printed all the features. So, then i put my code in and got the following error. After reading it I went to the Settings page of the Feature and made sure Add, Update, and Delete Features button was checked. Then I ran the same cell, but it threw the same error.
I ran the following successfully:
import arcpy, requests, json
# Disable warnings
requests.packages.urllib3.disable_warnings()
# Search cursor for feature service
fc = "https://services.arcgis.com/fGsbyIOAuxHnF97m/ArcGIS/rest/services/service_0a8c947fd081446eb3a31ef7ca0b966a/FeatureServer/0"
fields = ['Email_Category', 'whats_your_question_about',
'tax_related_question', 'zoning_or_flood_zone_question']
with arcpy.da.UpdateCursor(fc, fields) as cursor:
for row in cursor:
if row[1] == 'tax_assessment':
row[0] = 'tax assessment'
elif row[2] == 'tax_bill':
row[0] = 'tax bill'
elif row[3] == 'flood_zone':
row[0] = 'flood zone'
cursor.updateRow(row)
del cursor
I cannot see from your screen shot, but I'm guessing it may be the Feature Service URL. Did you append the /0 at the end?
Here's how I have the url.
fc = "https://services.arcgis.com/fGsbyIOAuxHnF97m/arcgis/rest/services/service_0a8c947fd081446eb3a31ef7ca0b966a/FeatureServer/0"
I didn't append /0 because I understood that to be the token if the feature wasn't shared.
Brilliant. My last notebook was pretty messy, so I started a whole new one. It worked great! Now I have to find a way to run this script from Jupyter via Task Scheduler.
Just an FYI, I usually prefer PyScripter. That will will create a .py file automatically. Take a look at the following document on how to configure PyScripter with Python 3.x:
Run PyScripter with ArcGIS Pro's Python 3.x and the ArcGIS API for Python
I find PyScripter is a littler easier to debug code. Also, take a look at the following to run a python scheduled task:
Scheduling a Python script or model to run at a prescribed time
For the python executable, that will be located at:
"C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe"
One other thing I just learned is you can schedule scripts/tools directly from ArcGIS Pro 2.5: