Search for a string with \ in the search

3772
9
Jump to solution
05-26-2015 04:56 PM
RebeccaStrauch__GISP
MVP Emeritus

This is part of a larger python script that is helping classify layers with broken links.  It's pretty close to being done, but I decided to break down my large Layer "other" group.  No need to get caught up in details (or simplifying syntax) right now, but I need help with the "\arc" and "\region" lines (18 and 21) below.  I'm assuming that \a and \r are special characters and therefore not seeing this as a string?  I did a search but didn't find a good resource even though I'm sure one exists.    I know \n is an issue - new line, but not sure if that is my problem here or not.

1 - where would I find a good list of special character for python; and

2 - how would I do a search for the "\arc" and "\region" strings?

if ".shp" in lyr.dataSource:  # shapes

    elif ".gdb" in lyr.dataSource:  # file gdb
        lyrType = "Fgdb"                            

    elif ".mdb" in lyr.dataSource:  # personal gdb or access file
        lyrType = "Pgdb"                            

    elif ".dbf" in lyr.dataSource:  # dbase format
        lyrType = "Dbf"                            

    elif ".txt" in lyr.dataSource:  # text file
        lyrType = "Txt"                            

    elif "\polygon" in lyr.dataSource:  # coverage polygon features
        lyrType = "Cover_poly"                            

    elif "\arc" in lyr.dataSource:  # coverage arc features
        lyrType = "Cover_arc"                            

    elif "\region" in lyr.dataSource:  # coverage region features
        lyrType = "Cover_region"                            

    elif "\point" in lyr.dataSource:  # coverage point features
        lyrType = "Cover_pts"                            

    elif ".sdc" in lyr.dataSource:  # shapes
        lyrType = "Esri.sdc"

    else: # pretty much evrything else includeing fgdb
        lyrType = "other"

Thanks!

0 Kudos
1 Solution

Accepted Solutions
SepheFox
Frequent Contributor

Have you tried putting an r before the quotation marks?

View solution in original post

0 Kudos
9 Replies
SepheFox
Frequent Contributor

Have you tried putting an r before the quotation marks?

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Thanks Sephe.  Never even thought about that in the case of search a string.  That worked perfect!

0 Kudos
SepheFox
Frequent Contributor

I found this reference: Python Strings. I don't see \p listed anywhere though?

DanPatterson_Retired
MVP Emeritus

​try this to get this output...then remove one of the slashes in the double-slash and see what happens..

>>> import string
>>> a = string.lowercase
>>> for i in a:  print("I am {0}, I am slash {1}".format(i,"\\"+i))
I am a, I am slash \a
I am b, I am slash \b
I am c, I am slash \c
I am d, I am slash \d
I am e, I am slash \e
I am f, I am slash \f
I am g, I am slash \g
I am h, I am slash \h
I am i, I am slash \i
I am j, I am slash \j
I am k, I am slash \k
I am l, I am slash \l
I am m, I am slash \m
I am n, I am slash \n
I am o, I am slash \o
I am p, I am slash \p
I am q, I am slash \q
I am r, I am slash \r
I am s, I am slash \s
I am t, I am slash \t
I am u, I am slash \u
I am v, I am slash \v
I am w, I am slash \w
I am x, I am slash \x
I am y, I am slash \y
I am z, I am slash \z

or more simply

>>> print "slash n","\n", "and stuff"

slash n

and stuff

>>> print "slash t","\t", " and stuff"

slash t and stuff

>>> print 
"slash n","\n", "and stuff"
slash n 
and stuff
>>> print "slash t","\t", " and stuff"
slash t and stuff
DanPatterson_Retired
MVP Emeritus

also "\\stuff" double backslashes = r"\morestuff"​

RebeccaStrauch__GISP
MVP Emeritus

Thanks Dan. Yep, either would work in my case.  I was just having a brain fart I guess because I typically think about those options when assigning a variable and not the what I was doing.  but of course that makes sense.  I knew you guys would set me straight in a flash!

0 Kudos
DanPatterson_Retired
MVP Emeritus

sometimes what I do is

>>> s = "c:\\stuff\\gis\\more\\stuff"

>>> s = s.replace("\\","/")

>>> s

'c:/stuff/gis/more/stuff'

curtvprice
MVP Esteemed Contributor

What a great chance to show off handling paths in the os module, and dictionaries!

# get file extension
ext = os.path.splitext(lyr.dataSource)[1].lower() # ".shp"
if ext:
    exts = [".shp", ".mdb", ".dbf", ".txt", ".sdc"]
    typs = ["Shp", "Pgdb", "Dbf", "Txt", "Esri.sdc"]
    dtype = dict(zip(exts, typs))
    try:
        lyrType = dtype[ext]
    except:
        lyrType = "Other"
else:
    # no extension
    base = os.path.basename(lyr.dataSource)
    if base in ["polygon", "arc", "region", "point"]:
        lyrType = "Cover_{}".format(base)
        lyrType = lyrType.replace("_polygon", "_poly").replace("_point", "_pts")
    else:
        lyrType = "Other"
RebeccaStrauch__GISP
MVP Emeritus

Hi Curtis, I'll take a look at this in the morning,  as mentioned, I haven't got to the point of cleaning up my script, and this is just one part...still trying to make sure I have all possible types covered.​  I'll know more by morning when I review the output.

0 Kudos