Python:如何使用存储在变量中的值?

时间:2015-09-17 05:18:17

标签: python jira

以下是代码段:

# Get details from the user.
jira_url = str(raw_input("JIRA URL: "))            # https://jira.mydomain.com
jira_prj = str(raw_input("JIRA Project: "))        # MYPROJ
jira_uid = str(raw_input("JIRA ID: "))             # admin
jira_pas = str(getpass.getpass("JIRA Password: ")) # p@s$wOrd!

# JIRA stuff.
jira = JIRA(server=jira_url, basic_auth=(jira_uid, jira_pas))
search_alert_issues = jira.search_issues("project = %s AND issuetype = Alert AND status = Submitted AND reporter in (anonymous)") % jira_prj
search_alert_issues

目的:

  1. 连接到JIRA服务器。
  2. 运行search_alert_issues
  3. 期望:

    >>> jira = JIRA(server='https://jira.mydomain.com', basic_auth=('admin', 'p@s$wOrd!'))
    >>> search_alert_issues = jira.search_issues("project = MYPROJ AND issuetype = Alert AND status = Submitted AND reporter in (anonymous)")
    >>> search_alert_issues
    [<JIRA Issue: key=u'MYPROJ-35460', id=u'129419'>, <JIRA Issue: key=u'MYPROJ-35459', id=u'129418'>, <JIRA Issue: key=u'MYPROJ-35458', id=u'129417'>, <JIRA Issue: key=u'MYPROJ-35457', id=u'129416'>, <JIRA Issue: key=u'MYPROJ-35456', id=u'129415'>, <JIRA Issue: key=u'MYPROJ-35455', id=u'129414'>, <JIRA Issue: key=u'MYPROJ-35454', id=u'129413'>, <JIRA Issue: key=u'MYPROJ-35453', id=u'129412'>, <JIRA Issue:     key=u'MYPROJ-35452', id=u'129411'>, <JIRA Issue: key=u'MYPROJ-35451', id=u'129410'>]
    >>>
    

    现实:

    Traceback (most recent call last):
      File "./ajat.py", line 82, in <module>
    search_alert_issues = jira.search_issues("project = %s AND issuetype = Alert AND status = Submitted AND     reporter in (anonymous)") % jira_prj
      File "/usr/lib/python2.7/site-packages/jira/client.py", line 1587, in search_issues
        resource = self._get_json('search', params=search_params)
      File "/usr/lib/python2.7/site-packages/jira/client.py", line 2033, in _get_json
        r = self._session.get(url, params=params)
      File "/usr/lib/python2.7/site-packages/jira/resilientsession.py", line 78, in get
        return self.__verb('GET', url, **kwargs)
      File "/usr/lib/python2.7/site-packages/jira/resilientsession.py", line 74, in __verb
        raise_on_error(r, verb=verb, **kwargs)
      File "/usr/lib/python2.7/site-packages/jira/utils.py", line 120, in raise_on_error
        r.status_code, error, r.url, request=request, response=r, **kwargs)
    jira.utils.JIRAError: JiraError HTTP 400
        text: Error in the JQL Query: The character '%' is a reserved JQL character. You must enclose it in a     string or use the escape '\u0025' instead. (line 1, character 11)
    

    问题:

    1. search_alert_issues应该可以使用jira_prj变量中存储的值,对吧?
    2. 但为什么它不起作用?当我对它们执行简单的打印命令时,所有jira_ *变量都显示它们的值。
    3. 我将如何使用jira_prj中存储的值?

2 个答案:

答案 0 :(得分:2)

search_alert_issues = jira.search_issues("project = %s AND issuetype = Alert AND status = Submitted AND reporter in (anonymous)") % jira_prj

% jira_prj应该在括号关闭之前出现 -

search_alert_issues = jira.search_issues("project = %s AND issuetype = Alert AND status = Submitted AND reporter in (anonymous)" % jira_prj)

现在,您要将project = %s AND issuetype = Alert AND status = Submitted AND reporter in (anonymous)发送到服务器,然后使用jira_prj中的值格式化结果。当Jira服务器看到%时,它会抱怨。

答案 1 :(得分:1)

% jura_prj位置错误

search_alert_issues = jira.search_issues("project = %s AND issuetype = Alert AND status = Submitted AND reporter in (anonymous)" % jira_prj)