Solved

how to check case insensitive substring

Posted on 2016-09-01
5
53 Views
Last Modified: 2016-09-03
I get a "string indices must be integers" error when I try either of the following,

 
partialUpper = workspacePath_base[firstIndex, UNC_Path.__len__()]

Open in new window


or

 
partialUpper = workspacePath_base[firstIndex, len(UNC_Path)]

Open in new window


Here is the original context that got me trying to figure this out,

 if UNC_Path.upper() in workspacePath_base[firstIndex, len(UNC_Path)].upper() :  

Open in new window


I know that I need to do something like

 if UNC_Path.upper() in workspacePath_base[0, 9].upper() :  

Open in new window


But that doesn't allow me to check dynamically.

This is Python 2.7
Thanks.
0
Comment
Question by:XTO
  • 3
  • 2
5 Comments
 
LVL 28

Expert Comment

by:pepr
ID: 41780578
Is it copied from a working code? Looking closer, don't you see something like...
if UNC_Path.upper() in workspacePath_base[firstIndex:len(UNC_Path)].upper():

Open in new window

...that is colon instead of coma inside the square brackets?

I guess that the workspacePath_base is of string type. If comma is used inside square brackets, it is converted to a tuple. You could rewrite it more explicitly as...
if UNC_Path.upper() in workspacePath_base[(firstIndex, len(UNC_Path))].upper():

Open in new window

(notice the extra parentheses). Then a string object would actualy want to say that you cannot use a tuple as an index.

On the other hand, the syntax like s[i1:i2] is called slicing in Python. For string objects, it says that you get substring of characters from index i1 (including), to index i2 (excluded).
0
 

Author Comment

by:XTO
ID: 41780730
It is an attempt at slicing. I want to see if the upper case version of the string, UNC_Path is contained within some segment of workspacePath_base.

I had also tried the simpler,
 if UNC_Path.upper() in workspacePath_base.upper()  :

Open in new window


But the debugger just stepped past it when there was a matching substring.

The end goal is to check to see if one substring is contained within the longer string, regardless of case.
0
 
LVL 28

Accepted Solution

by:
pepr earned 500 total points
ID: 41781071
Do print(repr(UNC_Path)) and the same for workspacePath_base just before the if to see if the strings really contain the values you expect. (Simply said, the repr() just adds the quotes around the string.) Do the same with the values changed to .upper().

For the UNC, you may want to use the .startswith() method instead of the in operator.
0
 

Author Comment

by:XTO
ID: 41781913
The .startswith() method worked and did the job.

Both of these worked:

        if workspacePath_base.startswith(NAS01a_Proj_Arcview) :

Open in new window

       

        if workspacePath_base.upper().startswith(NAS01a_Proj_Arcview.upper() ) :

Open in new window

0
 
LVL 28

Expert Comment

by:pepr
ID: 41782987
Actually, the .startswith() does not do the case-insensitive testing. For the paths, you may want to use os.path.normcase(). You may also be interested in os.path.splitdrive(). You may find also other methods (in the documentation) that may be nice to use in your case.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now