这里打印列表内容的标准错误的语句是情景:我怎样才能在Python
我希望能够打印的空调风格声明:
print >> sys.stderr, ("%s does not exist"%m_args)
无论是里面的功能,或在__main__
,但我得到异常:
print >> sys.stderr, ("%m_args[1] does not exist"%m_args[1])
IndexError: list index out of range
代码:
#!/usr/bin/env python
import re, os, sys, jira, subprocess
from optparse import OptionParser
import warnings
from collections import namedtuple
global m_args
def verify_commit_text(tags):
for line in tags:
if re.match(r'[^\NO-TIK]',line):
return False
elif re.match(r'[^\NO-REVIEW]', line):
return False
elif re.match(r'[a-zA-Z]+-\d+', line):
# Validate the JIRA ID
m = re.search("([a-zA-Z]+-\d+)",line)
m_args = m.group(1)
m_args = [m_args]
print 'm_args'
print m_args
print type(m_args)
if CheckForJiraIssueRecord(m_args):
return False
else:
#warnings.warn("%s does not exist"%m_args)
print >> sys.stderr, ("%s does not exist"%m_args)
return True
else:
return True
def CheckForJiraIssueRecord(my_args):
# turn off stdout
#sys.stdout = open(os.devnull)
#sys.stderr = open(os.devnull)
com = jira.Commands()
logger = jira.setupLogging()
jira_env = {'home':os.environ['HOME']}
command_name = "cat"
server = "http://jira.server.com:8080/rpc/soap/jirasoapservice-v2?wsdl"
options = namedtuple('Options', 'user password')('user','password')
jira.soap = jira.Client(server)
jira.start_login(options, jira_env, command_name, com, logger)
issue = com.run(command_name, logger, jira_env, my_args)
if issue:
return True
if __name__ == '__main__':
commit_text_verified = verify_commit_text(os.popen('hg tip --template "{desc}"'))
if commit_text_verified:
sys.exit(1)
else:
print >> sys.stderr, ('[obey the rules!]')
print >> sys.stderr, ("%s does not exist"%m_args[0])
sys.exit(0)
很明显,因为'm_args [1]'不存在,试图访问它会引发一个'IndexError',当你试图访问它时,因为它不存在!你想要打印什么? –
from 3 lines: m = re.search(“([a-zA-Z] + - \ d +)”,line) m_args = m.group(1) m_args = [m_args] 我想打印输出“m-args” – kamal
对不起,这只是一个小实验,用[0]代替[1],否则代码匹配如果我使用打印>> sys.stderr,(“%s不存在”%m_args) 在__main__我得到.... []不存在 – kamal