Removing characters from a Feature Class file name

3507
12
Jump to solution
02-14-2021 01:25 PM
MichaelTomiak
New Contributor III

This should be a simple answer ... I'm trying to just remove the first 25 characters from a list (>90 in number) of Feature classes (raster datasets) within a .gdb.

All the file names are named the same except for the last 9 digits (hence why I am trying to remove the first 25).
I would then like to add some standard text to all the FCs (in the example "Hello")

 

My attempt from other threads comprises the following (but has not worked so far):

-------------------

import arcpy
import os

dir = r'C:\Users\X\Desktop\X\X.gdb'


arcpy.env.workspace = dir

for ras in arcpy.ListRasters ("*"):
print (ras)    #original raster name
basename = ras.split("*")[:25]    #strips the first 25 characters from original raster name
newrastername = "Hello" + basename
print (newrastername)
arcpy.CopyRaster_management(ras, newrastername)   #create a new raster with the new/updated name

----------------------

Error received:

---------------------------------------------------------------------------
SyntaxError                               Traceback (most recent call last)
File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\ast.py, in parse:
Line 35:    return compile(source, filename, mode, PyCF_ONLY_AST)

SyntaxError: Missing parentheses in call to 'print'. Did you mean print(ras #original raster name)? (<string>, line 😎

 

0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor
import arcpy
import os

dir = r'C:\Users\X\Desktop\X\X.gdb'


arcpy.env.workspace = dir

for ras in arcpy.ListRasters ("*"):
    #print (ras)    #original raster name
    #print(ras.split("*"))
    basename = ras[25:]    #strips the first 25 characters from original raster name
    newrastername = "Hello" + basename
    print (newrastername)
    arcpy.CopyRaster_management(ras, newrastername)   #create a new raster with the new/updated name

View solution in original post

12 Replies
DavidPike
MVP Frequent Contributor

Can you share your print statement returns and your error messages?  It looks ok, but what is ras.split("*") for?

0 Kudos
MichaelTomiak
New Contributor III

yes apologies

 

It is to try to remove the first 25 (took the split from some research on line, but likely not the most sensible function to use)

 

0 Kudos
DavidPike
MVP Frequent Contributor

well that's not quite right.  Just use 

basename = ras[:25]

and forget about the .split("*") unless you want to create a list of strings split at an asterisk *.  in that case use

basename = ras.split("*")[0][:25]

 

Edit - I also think you need [25:] not [:25]

DanPatterson
MVP Esteemed Contributor

split produces pieces, you have to determine which one first then slice off the characters.  For example

 

 

ras = "left*right"

ras.split("*")[0][:2]
'le'

ras.split("*")[1][:2]
'ri'

ras.split("*")[0][-2:]
'ft'

ras.split("*")[1][-2:]
'ht'

or skip the split

ras[-5:]
'right'

ras[:5]
'left*'

 

 


... sort of retired...
MichaelTomiak
New Contributor III

thank you. However I'm still getting an 'indentation' error:

IndentationError                          Traceback (most recent call last)
File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\ast.py, in parse:
Line 35:    return compile(source, filename, mode, PyCF_ONLY_AST)

IndentationError: expected an indented block (<string>, line 10)

 

Original code:

import arcpy
import os

dir = r'C:\Users\Michael.Tomiak\Desktop\25\Copy.gdb'


arcpy.env.workspace = dir

for ras in arcpy.ListRasters ("*"):
print (ras)
basename = ras[25:] #strips the first 25 characters from original raster name
newrastername = basename
print (newrastername)
arcpy.CopyRaster_management(ras, newrastername) #create a new raster with the new/updated name

0 Kudos
DavidPike
MVP Frequent Contributor
import arcpy
import os

dir = r'C:\Users\X\Desktop\X\X.gdb'


arcpy.env.workspace = dir

for ras in arcpy.ListRasters ("*"):
    #print (ras)    #original raster name
    #print(ras.split("*"))
    basename = ras[25:]    #strips the first 25 characters from original raster name
    newrastername = "Hello" + basename
    print (newrastername)
    arcpy.CopyRaster_management(ras, newrastername)   #create a new raster with the new/updated name
MichaelTomiak
New Contributor III

Thanks. Worked well. Didn't know indentation was required for it to run. Thought it was a presentation aspect. 

 

WRT to the last line of code, what function could replace this so that it was only renaming the original files, rather than making a copy of the data (which is useful sometimes) but I do not have much data storage capacity left. Making a copy might be tight.

 
Would it be something like:
arcpy.RenameRaster_management (ras, newrastername)

0 Kudos
DavidPike
MVP Frequent Contributor
import arcpy
#import os #youre not using os so dont import it

dir = r'C:\Users\X\Desktop\X\X.gdb'


arcpy.env.workspace = dir

for ras in arcpy.ListRasters ("*"):
    #print (ras)    #original raster name
    #print(ras.split("*"))
    basename = ras[25:]    #strips the first 25 characters from original raster name
    newrastername = "Hello" + basename
    print (newrastername)
    arcpy.Rename_management(ras, newrastername)   #rename raster with the new/updated name
MichaelTomiak
New Contributor III

Worked a treat 😀- thanks for your time and effort David.  Saved me an awful lot of manual renaming tonight.

 

0 Kudos