在循环内调用函数在 Python 中不起作用

Call a function inside a loop is not working in Python

我有一个 function/method 可以创建 JIRA 票证。我想在循环中调用该函数,以便我可以将不同的描述传递到 JIRA 票证中。我有一个包含不同服务失败的 CSV 文件,所以我的想法是为 csv 文件中的每一行创建 JIRA。

我的 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

我从另一个 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])

我的 CSV 数据如下所示

PDFDownloader,Backend failed,100
ImageProcess,NullPointer,200

所以 jira_creation() 方法调用了 jira_rest_call() 并仅在第一行创建了一张票,但我期待两张票。

这段代码有什么问题?

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

我什至测试了打印语句(print([i])),它打印了两次但是方法调用(jira_rest_call( [i])) 只发生过一次。

您读取 csv 文件的方式不正确。试试这个:

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.