0

I have the following piece of code:

self.ignore_dir_extensions = ['xcodeproj']

item = repr(extension.split('/')[0])

print "String: " + item

if item in self.ignore_dir_extensions:
    print "Available: " + item

Let's say I have this output:

String: 'xcodeproj'

Expected output:

String: 'xcodeproj'
Available: 'xcodeproj'

Could anyone help me out here?

1
  • what the question is? can you give the smaller functional piece of code related to your problem?. what extension is?
    – joaquin
    Commented Apr 22, 2011 at 23:39

4 Answers 4

3

Try the following:

self.ignore_dir_extensions = ['xcodeproj']

item = extension.split('/')[0]

print "String: " + repr(item)

if item in self.ignore_dir_extensions:
    print "Available: " + repr(item)

You do not want to have item be the result of repr(), because repr() on a string will add quotes, for example:

>>> repr("xcodeproj")
"'xcodeproj'"
>>> print repr("xcodeproj")
'xcodeproj'
>>> print "xcodeproj"
xcodeproj

When you are checking to see if the string exists in the list, you don't want the quotes unless the string you are trying to match also has them.

0
3

Your test for is-string-contained-in-list is correct. However, you are testing for the presence of a different string than you intended. You called repr on your string, so the name item is bound to the string "'xcodeproj'" (not to the string "xcodeproj").

0

I would personally do this:

self.ignore_dir_extensions = ['xcodeproj']

item = repr(extension.split('/')[0])

print "String: " + item

try:
    self.ignore_dir_extensions.index(item)
    print "Available: " + item
except:
    pass
1
  • Though that doesn't actually fix your problem, I'd go with Croad's answer.
    – Cthos
    Commented Apr 22, 2011 at 23:45
0

you can simply use:

item = extension.split('/')[0]

print "String: '%s'" % item

if item in self.ignore_dir_extensions:
    print "Available: '%s'" %` item

this way you avoid the problems with repr

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.