I need to remove forward slashes from the attributes in a layer. I need to remove the first \ and last \ and replace the double \\ with just one \. I would appreciate any help. Thanks.
Currently the attributes looks like this.
\R2\\M1\
\M1\\M1\\C2\
\AG\\R2\\M1\\R2\
I need it to look like the following
R2\M1
M1\M1\C2
AG\R2\M1\R2
I tried the following but got error "TypeError: argument of type 'NoneType' is not iterable" on line if '\\' if '\\' in row[0]: #if a double quote is in the string returned.
with arcpy.da.UpdateCursor("In_memory\LayerTest", "ZONING_CODE") as cursor:
for row in cursor:
if '\\' in row[0]: #if a double quote is in the string returned,
row[0]=row[0].replace('\\','\\') #replace that double quote with an empty string (removes it)
cursor.updateRow(row)
del row
Solved! Go to Solution.
Does the following do what you want:
with arcpy.da.UpdateCursor("In_memory\LayerTest", "ZONING_CODE") as cursor:
for code, in cursor:
if code:
cursor.updateRow([code.strip("\\").replace("\\\\","\\")])
throw a print statement in between line 2 and 3... it appears that 'None' (aka a null) is being returned for row[0].
Also in python ...
a = "\\"
a.replace("\\", "")
a = "\\\\"
a.replace("\\\\", "\\")
Out[16]: '\\'
I put a print row in between lines 2 & 3.
with arcpy.da.UpdateCursor(ZT1, "ZONING_CODE") as cursor:
for row in cursor:
print row
if '\\\\' in row[0]: #if a double quote is in the string returned,
row[0]=row[0].replace('\\\\','\\') #replace that double quote with an empty string (removes it)
cursor.updateRow(row)
del row
I get alot of
[u'\\AG\\']
[u'\\AG\\\\C1\\']
[u'\\R1\\']
[u'\\RR\\']
[u'\\AG\\\\RR\\']
[u'\\AG\\']
[u'\\AG\\\\RR\\']
[u'\\CR-M1\\']
Traceback (most recent call last):
File "C:\GIS\Python\ParcelTool\SpatialJoinTest.py", line 4, in <module>
if '\\\\' in row[0]: #if a double quote is in the string returned,
TypeError: argument of type 'NoneType' is not iterable
Yes a Null is being returned.
You may want to wrap another if around the row[0] checking.
if row[0] or len(row[0]) > 0:
do you mean an elif, i am not sure i am understand how to wrap another if around the row[0]
with arcpy.da.UpdateCursor(ZT1, "ZONING_CODE") as cursor:
for row in cursor:
if '\\\\' in row[0]: #if a double quote is in the string returned,
if row[0] or len(row[0]) > 0:
row[0]=row[0].replace('\\\\','\\') #replace that double quote with an empty string (removes it)
cursor.updateRow(row)
I was able to remove the double "\\" from within attributes . It didn't how ever remove the \ in the begging or the end. I would appreciate any help please.
\R2\\M1\ ----> \R2\M1\
\M1\\M1\\C2\ -----> \M1\M1\C2\
\AG\\R2\\M1\\R2\ -----> \AG\R2\M1\R2\
Current Code
with arcpy.da.UpdateCursor("In_memory\LayerTest", "ZONING_CODE") as cursor:
for row in cursor:
#print row
#if row[0] or len(row[0]) > 0:
if "\\\\" in row[0]: #if a double quote is in the string returned,
for row in cursor:
if row[0] or row[0] > 0:
row[0]=row[0].replace("\\\\","\\") #replace that double quote with an empty string (removes it)
cursor.updateRow(row)
del row
you could slice before you update as in this example
row0 = 'abcdef'
row0 = row0[1:-1]
row0
'bcde'
apologies for my slowness. I tried the following but got error on line 7.
cursor.updateRow([row[0][1:-1]])
TypeError: 'NoneType' object has no attribute '__getitem__'
ZT1 = "In_memory\ZoningTest1"
arcpy.MakeFeatureLayer_management("In_memory\ZoningTest", ZT1)
with arcpy.da.UpdateCursor(ZT1, "ZONING_CODE") as cursor:
for row in cursor:
print row
cursor.updateRow([row[0][1:-1]])
del row
with arcpy.da.UpdateCursor(ZT1, "ZONING_CODE") as cursor:
for row in cursor:
#print row
#if row[0] or len(row[0]) > 0:
if "\\\\" in row[0]: #if a double quote is in the string returned,
for row in cursor:
if row[0] or row[0] > 0:
row[0]=row[0].replace("\\\\","\\") #replace that double quote with an empty string (removes it)
cursor.updateRow(row)
del row
I have no clue why you have to two cursors going on.
Presumably in and around line 17, row[0] is returning a string, which you then use the replace thing to get rid of the double quotes, which you then reassign back to row[0]. In line 17, why don't you call it 'val', so
val = row[0].replace(blah blah)
row[0] = val[1:-1]
cursor.updateRow(row)
looks like it kind of worked....? it removed some of the \ but not all. See attached pic
with arcpy.da.UpdateCursor(ZT1, "ZONING_CODE") as cursor:
for row in cursor:
#print row
#if row[0] or len(row[0]) > 0:
if "\\\\" in row[0]: #if a double quote is in the string returned,
for row in cursor:
if row[0] or row[0] > 0:
val = row[0].replace("\\\\","\\") #replace that double quote with an empty string (removes it)
row[0]=val[1:-1]
cursor.updateRow(row)
del row