Python 如何在函数调用中有条件地向字典添加键/值对?
我试图从Jira数据库中检索数据并保存到熊猫数据框中。这是我的密码:Python 如何在函数调用中有条件地向字典添加键/值对?,python,pandas,dataframe,Python,Pandas,Dataframe,我试图从Jira数据库中检索数据并保存到熊猫数据框中。这是我的密码: from jira import JIRA import pandas as pd cert_path = 'C:\\cert.crt' start_date = '2020-10-01' end_date = '2020-10-31' # three different instances (each with their own schema) a_session = JIRA(server='https://jir
from jira import JIRA
import pandas as pd
cert_path = 'C:\\cert.crt'
start_date = '2020-10-01'
end_date = '2020-10-31'
# three different instances (each with their own schema)
a_session = JIRA(server='https://jira.myinstance-A.com', options={'verify': cert_path}, kerberos=True)
b_session = JIRA(server='https://jira.myinstance-B.com', options={'verify': cert_path}, kerberos=True)
c_session = JIRA(server='https://jira.myinstance-C.com', options={'verify': cert_path}, kerberos=True)
# define Jira queries
query_1 = 'project = \"Test Project 1\" and issuetype = Incident and resolution = Resolved and updated >= {} and updated <= {}'.format(start_date, end_date)
query_2 = 'project = \"Test Project 2\" and issuetype = Incident and resolution = Resolved and updated >= {} and updated <= {}'.format(start_date, end_date)
query_3 = 'project = \"Test Project 3\" and issuetype = Defect and resolution = Resolved and releasedate >= {} and releasedate <= {}'.format(start_date, end_date)
query_4 = 'project = \"Test Project 4\" and issuetype = Enhancement and resolution = Done and completed >= {} and completed <= {}'.format(start_date, end_date)
# fetch all issues from a given session for a given query
block_size = 100
block_num = 0
def get_all_issues(session, query):
block_size = 50
block_num = 0
start = 0
all_issues = []
while True:
issues = session.search_issues(query, start, block_size)
if len(issues) == 0:
# No more issues
break
start += len(issues)
for issue in issues:
all_issues.append(issue)
issues = pd.DataFrame(issues)
for issue in all_issues:
d = {
'jira_key' : issue.key,
'issue_type' : issue.fields.type,
'creator' : issue.fields.creator,
'resolution' : issue.fields.resolution
}
issues = issues.append(d, ignore_index=True)
return issues
# list of queries, and the corresponding backend
queries = [
(a_session, query_1),
(a_session, query_2),
(b_session, query_3),
(c_session, query_4),
]
# loop over each pair of session and query, calling the get_all_issues function, and save the dataframe we get each time
dataframes = []
for session, query in queries:
dataframe = get_all_issues(session, query)
dataframes.append(dataframe)
# concatenate all data frames
all = pd.concat(dataframes)
自定义_字段_123
存在于a_会话
和b_会话
中,但不存在于c_会话
中
custom_field_456
仅存在于c_会话中
而且,custom\u field\u 789
存在于b\u会话
和c\u会话
中
使用此扩展字典运行代码会导致以下错误:AttributeError:type对象“PropertyHolder”没有属性“custom\u field\u 123”。
我试图在所有问题中的问题之前添加一个IF
语句:,但这种方法不起作用
是否有办法根据字段是否与给定的
会话相关,将键/值对添加到d
字典中?例如,如<代码> AyStudio/QueRy2.2/代码>正在运行,只考虑<代码> CuthiFieldField123(因为这是唯一有效的)和<强> >忽略< <强> > CuthyField466和(因为通过这些会导致上面的错误消息).我想您需要单独添加它们。比如:
d = {
'key' : issue.jira_key,
'type' : issue.fields.issue_type,
'creator' : issue.fields.creator,
'resolution' : issue.fields.resolution
}
fields = issue.fields # For brevity
if hasattr(fields, "custom_field_123"):
d['system_change'] = fields.custom_field_123
if hasattr(fields, "custom_field_456"):
d['system_resources'] = fields.custom_field_456
if hasattr(fields, "custom_field_789"):
d['system_backup'] = fields.custom_field_789
但是,如果您使用的是Python 3.8+,并且字段值从不为空/无,则可以使用和默认值来减少重复:
if value := getattr(fields, "custom_field_123", None):
d['system_change'] = value
if value := getattr(fields, "custom_field_456", None):
d['system_resources'] = value
if value := getattr(fields, "custom_field_789", None):
d['system_backup'] = value
我想你需要分别添加它们。比如:
d = {
'key' : issue.jira_key,
'type' : issue.fields.issue_type,
'creator' : issue.fields.creator,
'resolution' : issue.fields.resolution
}
fields = issue.fields # For brevity
if hasattr(fields, "custom_field_123"):
d['system_change'] = fields.custom_field_123
if hasattr(fields, "custom_field_456"):
d['system_resources'] = fields.custom_field_456
if hasattr(fields, "custom_field_789"):
d['system_backup'] = fields.custom_field_789
但是,如果您使用的是Python 3.8+,并且字段值从不为空/无,则可以使用和默认值来减少重复:
if value := getattr(fields, "custom_field_123", None):
d['system_change'] = value
if value := getattr(fields, "custom_field_456", None):
d['system_resources'] = value
if value := getattr(fields, "custom_field_789", None):
d['system_backup'] = value
我希望system\u change
键/值对在不存在/应用的情况下不存在。例如,system\u change
在c\u会话中不存在,因此不应被传递。我希望system\u change
键/值对在不存在/应用的情况下不存在。例如,system\u change
在c\u会话中不存在,因此不应传递。