這裏打印列表內容的標準錯誤的語句是情景:我怎樣才能在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