Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Is there an arcgisscripting method for sanitizing a filename to get rid of characters which Arcgis barfs on, the meek hyphen - for example?

share|improve this question
add comment

3 Answers

up vote 6 down vote accepted

It turns out there is: ValidateTableName_method and here is how one could use it:

  import arcgisscripting
  gp = arcgisscripting.create()
  outshape = 'c:/temp/%s.shp' % gp.ValidateTableName('My-Merge-Output')
  print outshape

  >>> c:/temp/My_Merge_Output.shp

One needs to be careful to only process the filename, and not include the path or extension else periods and slashes will be replaced also:

  outshape = gp.ValidateTableName('c:/temp/My-Merge-Output.shp')
  print outshape

  >>> c__temp_My_Merge_Output_shp

Don't run validate on an input file though. Some experiments showed that a hyphenated filename is legal on input and illegal on output. Also see the related ValidateFieldName_method

If you need to process path and filename you might try this (see this ESRI Forum thread):

import os, string, arcgisscripting
gp = arcgisscripting.create()

def validateName(inputTablePath):
    workspacePath = string.join(inputTablePath.split(os.sep)[0:-1], os.sep)
    if inputTablePath[-4:] in (".shp",".dbf",".txt"):
        tableNameToValidate = inputTablePath.split(os.sep)[-1][0:-4]
        return workspacePath + os.sep + gp.validatetablename(tableNameToValidate, workspacePath) + inputTablePath[-4:]
    else:
        tableNameToValidate = inputTablePath.split(os.sep)[-1]
        return workspacePath + os.sep + gp.validatetablename(tableNameToValidate, workspacePath)

print validateName(r"C:\temp\testthis\why-not-this.shp")
print validateName(r"C:\temp\testthis\_*howboutwhy-not-this.shp")
print validateName(r"C:\temp\testthis\why-not-this.dbf")
print validateName(r"C:\temp\testthis.gdb\why-not-this")


#OUTPUT >>>
#C:\temp\testthis\why_not_this.shp
#C:\temp\testthis\__howboutwhy_not_this.shp
#C:\temp\testthis\why_not_this.dbf
#C:\temp\testthis.gdb\why_not_this

Thank you Chris Snyder, Conrad J. Wyrzykowski, Luke Pinner, Jason Scheirer, Peter Siebert for contributing to this answer and Matt's education. (ESRI-L)

share|improve this answer
add comment

A snippet someone gave to me..

specChar_lookup = {
    "&" : "and", "/" : " "
    }

def sanatize(string):
    for char in specChar_lookup:
        string = string.replace(char, specChar_lookup[char])
    return string


sc = gp.searchcursor(shp)
row = sc.next()
while row:
    #altline = row.getvalue("MULTI_TW.LineLoop.ALTLINENAME")
    linename = sanatize(row.getvalue("Producer"))###Get the Values in the field 'LINENAME'
    if linename not in rowlist:
        rowlist.append(linename)
    row = sc.next()
del row
share|improve this answer
add comment

There is no method for this in arcgisscripting, but you can just use good old Python string manipulation methods to get the job done, such as replace (replace "bad" strings with "").

http://docs.python.org/library/string.html

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.