简体   繁体   English

在循环内调用 function 在 Python 中不起作用

[英]Call a function inside a loop is not working in Python

I have a function/method which will create a JIRA ticket.我有一个可以创建 JIRA 票证的函数/方法。 I want to call that function inside a loop so that i can pass different descriptions into the JIRA ticket.我想在循环中调用 function 以便我可以将不同的描述传递到 JIRA 票证中。 I have a CSV file with different service failures so the idea is to create JIRA for each row from the csv file.我有一个 CSV 文件,其中包含不同的服务故障,因此我的想法是为 csv 文件中的每一行创建 JIRA。

My JIRA Method我的 JIRA 方法

def jira_rest_call(description):
    # Build the text for the JIRA ticket.
    jira_summary = "Pro active Monitoring"
    jira_assignee='USERID'
    jira_description = description
    priority = 'High'
    labels_list = 'Production Failure';
    
    # Build the JSON to post to JIRA
    json_data = '''
    {
        "fields":{
            "project":{
                "id": "25102",
                "key": "ABC"
                
            },
            "assignee":{"name":"%s"},
            "summary": "%s",
            "issuetype":{
                "name":"Story"
            },
            "description": "%s",
            "priority":{"name":"%s"},
            "labels":["%s"]
        } 
    }''' % (jira_assignee,jira_summary, jira_description,priority,labels_list)
   
   # Set the root JIRA URL, and encode the username and password 
    url = 'https://jira-abb.net/rest/api/2/issue'
  
    userpass = 'Z683050' + ':' + '*******'
    encoded_u = base64.b64encode(userpass.encode()).decode()
    headers = {"Authorization" : "Basic %s" % encoded_u}
    headers={'Content-Type':'application/json'}
    # Build the request
    r = requests.post(url,auth=HTTPBasicAuth('Z683050', ''*******'), headers=headers, data=json_data) 
    # Send the request and grab JSON response
    # response = urlopen(restreq, data)

      # Load into a JSON object and return that to the calling function
    return r

I am calling this method from a different Python module like this -我正在从不同的 Python 模块调用此方法,如下所示 -

def jira_creation():
    with open ('test_duplicates_1.csv','r') as csv_file:
        for i in csv_file:
            print([i])
            jira_rest_call([i])

My CSV Data looks like this我的 CSV 数据看起来像这样

PDFDownloader,Backend failed,100
ImageProcess,NullPointer,200

So jira_creation() method has invoked jira_rest_call() and created a ticket only with the first line but i am expecting two tickets.所以jira_creation()方法调用jira_rest_call()并仅使用第一行创建了一张票,但我期待两张票。

What is wrong in this code?这段代码有什么问题?

with open ('test_duplicates_1.csv','r') as csv_file:
            for i in csv_file:
                print([i])
                jira_rest_call([i])

I even tested the print statement( print([i]) ), its printing two times but the method call( jira_rest_call([i]) ) has happened only once.我什至测试了打印语句( print([i]) ),它打印了两次,但方法调用( jira_rest_call([i]) )只发生了一次。

You are not reading the csv file the right way.您没有以正确的方式阅读 csv 文件。 Try this instead:试试这个:

with open ('test_duplicates_1.csv','r') as csv_file:
  reader = csv.DictReader(csv_file)
    for row in reader:
      print(row)
      jira_rest_call(row['<column_name>'])  # I don't know what the column names are in your csv file. Replace with the right one.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM