Select to view content in your preferred language

Python script to remove spaces in notebook

1123
4
12-18-2020 11:52 AM
Labels (1)
JonathanWhite1
Occasional Contributor

I'm having real trouble with getting a script to run in notebook to simply remove spaces from a field.

item=gis.content.get('528e3960da004561999c50dcaa52a781')
l=item.layers[0]

print(l.calculate(where="OBJECTID > 0",
calc_expression={"field": "test2", "sqlExpression": "Replace(test2," ","")"}))

This fails and upon further investigation the REPLACE SQL function is not supported. So I need a python script to run and remove spaces in a hosted feature layer's field. The reason why is a long story but this has to be running at least every 30 minutes 24/7, for the extent of COVID..  The field I am trying to remove spaces from is called test2. If I can get something to work then ill use the code on the actual feature layer.

I pretty much have no idea what I am doing with notebook. 

0 Kudos
4 Replies
DavidPike
MVP Notable Contributor

The only thing I could find was this:

python - How can I use UpdateCursor in AGOL layers? - Geographic Information Systems Stack Exchange

 

Using that:

 

 

#example from link
g = GIS("https://www.arcgis.com", USER, PASS)

token = g._con.token

fs = g.content.search(query="title:My Service", max_items=10)
url = fs[0].layers[0].url
print(url)

featureset = arcpy.FeatureSet()
authenticatedURL = url + "?token={}".format(token)
featureset.load(authenticatedURL)

with arcpy.da.UpdateCursor(authenticatedURL , "*") as cursor:
    for row in cursor:
        row[0] = "hello"
        cursor.updateRow(row)


#what you might try, definitely won't work
#first time, but you may be able to adjust it

g = GIS("https://www.arcgis.com", USER, PASS)

token = g._con.token

item = gis.content.get('528e3960da004561999c50dcaa52a781')
l=item.layers[0]
url = l.url
print(url)

featureset = arcpy.FeatureSet()
authenticatedURL = url + "?token={}".format(token)
featureset.load(authenticatedURL)

#run a cursor on field 'test2'
with arcpy.da.UpdateCursor(authenticatedURL , "test2") as cursor:
    for row in cursor:
        row[0] = row[0].replace(" ","")
        cursor.updateRow(row)

 

JonathanWhite1
Occasional Contributor

Yea its saying arcpy is not defined.

0 Kudos
DavidPike
MVP Notable Contributor

Do you have to do it in the notebook? Can you import arcpy in the notebook (I'm not really conversant in the notebook interface)?

 

Outside a notebook I gues you would need

from arcgis.gis import GIS

import arcpy

 

'yea' isn't a great reply to someone who spent time to assist you.

0 Kudos
JonathanWhite1
Occasional Contributor

I have to automate it in AGOL so as far as I know the only way would be through using notebook? This is the first time I have used python at all. Yea I think I am in over my head but do not have much of a choice due to some things not being supported in Survey 123. The code you provided is starting to make sense as far as how things are being defined and later called. I am thinking that arcpy might already be in the AGOL notebook so perhaps it doesn't need to be imported or called at all. Thanks for your help. I will just keep trying stuff until something serious breaks.

0 Kudos