Python compatability issue - 'in <string>' requires character as left operand
I make no claims to know anything at all about writing Python scripts or programming in general, but I've been tasked with writing one anyway that will have to operate on a wide variety of Python versions. I wrote, and did my testing on versions 2.3, 2.4 and all went well. Version 2.2 though is giving me fits and my google-fu must be weak because I'm coming up short with an answer.
The section of code that's giving me problems:
commandnum = 0
for commandloop in arguments[0:]:
if "java" in arguments[commandnum][0]:
for argloop in arguments[commandnum]:
if "Dcatalina.home" in argloop:
instance = argloop.split("/")
elif "-Xms" in argloop:
xms = argloop.split("Xms")
elif "-Xmx" in argloop:
xmx = argloop.split("Xmx")
elif "-XX:MaxPermSize" in argloop:
permsize = argloop.split("=")
elif "Dcatalina.base" in argloop:
home = argloop.split("=")
appdir = home[-1]+"/webapps"
warfiles = []
for war in os.listdir(appdir):
if fnmatch.fnmatch(war, '*.war'):
warfiles.append(war)
thefiles = ",".join(warfiles)
try:
instance
except NameError:
instance = None
if instance is not None:
print "%s %s %s %s %s %s" % (instance[-2], xms[-1], xmx[-1], permsize[-1], appdir, thefiles)
commandnum = commandnum + 1
I understand that this is UGLY and probably a BAD thing, but the error that I'm getting is in the string matching using 'in'. From what I gather by googling, in Python 2.2 you're limited to one character for string matching using 'in'. What is the equivalent way in 2.2 to match on a string?
Upgrading machines to a modern version of Python is out of the question.
Any help/abuse is appreciated. Try to be gentle as this is the first thing of any size I've written开发者_如何学运维 outside of shell scripts and MS Basic on my TRS-80 CoCo in 1981.
Try using str.find
, which returns -1 if the substring cannot be found.
>>> s = "The quick brown fox"
>>> s.find("The")
0
>>> s.find("brown")
10
>>> s.find("waffles")
-1
You can always match using regular expressions. It's been a while since I used 2.2, so I can't recall if re
is available, or if you need to go back to regex
, but I'm sure the docs on the python site will go back far enough.
Update: it looks like re is available.
精彩评论