我正在从JSON文件中读取数据,并且我尝试根据键(导演+ sprintname)附加三个计算总体saydo,已提交的saydo和额外的saydo。我收到下面的错误。 TypeError:append()仅接受一个参数(给定2个参数)
代码:
import json
with open('C:/Users/erpsaam/Desktop/report-v89/kpi-report-aggregation-by-
director-2018-08-31.json') as f:
category_director_json = json.load(f)
category_director_dataset = [] # we will fill this in this script
for row in category_director_json:
row.get('director',None)
row.get('sprintName', None)
row.get('overallSayDo', None)
row.get('commitedSayDo', None)
row.get('extraSayDo', None)
category_director_dataset.append("DIRECTOR+SPRINTNAME", {'overallSayDo': 'overallSayDo_value', 'commitedSayDo': 'commitedSayDo_value', 'extraSayDo': 'extraSayDo_value'})
print(json.dumps(category_director_dataset))
样本数据(JSON文件)
[
{
"teamName":"EPAM Backend 1,Cloud Engineering,Content Infrastructure Montreal,Jedi,Seal Team India - APIs,Seal Team India - Security,Seal Team X,Seal Team X India,Sith,UC Live,WeRTV,Krazy Kanucks,Seal Team India - Core,Operator Portal",
"sprintName":"MF-S98",
"director":"Rajesh",
"locations":"Ukraine,SC,Duluth,India,Montreal,LA",
"countries":"U.S.,Ukraine,Canada,Serbia,Spain,India",
"cities":"Santa Clara,Ontario Quebec,Duluth,Bangalore,L.A.",
"vendors":"EPAM,FTE,ITC,L&T TS,G&L,Phalita,Assistera",
"overallSayDo":17.43,
"commitedSayDo":9.86,
"extraSayDo":18.14
},
{
"teamName":"EPAM Backend 1,Cloud Engineering,Content Infrastructure Montreal,Jedi,Seal Team India - APIs,Seal Team India - Security,Seal Team X,Seal Team X India,Sith,UC Live,WeRTV,Krazy Kanucks,Seal Team India - Core,Operator Portal",
"sprintName":"MF-S97",
"director":"Rajesh",
"locations":"Ukraine,SC,Duluth,India,Montreal,LA",
"countries":"U.S.,Ukraine,Canada,Serbia,Spain,India",
"cities":"Santa Clara,Ontario Quebec,Duluth,Bangalore,L.A.",
"vendors":"EPAM,FTE,ITC,L&T TS,G&L,Phalita,Assistera",
"overallSayDo":60,
"commitedSayDo":49.36,
"extraSayDo":37.57
},
{
"teamName":"EPAM Backend 1,Cloud Engineering,Content Infrastructure Montreal,Jedi,Seal Team India - APIs,Seal Team India - Security,Seal Team X,Seal Team X India,Sith,UC Live,WeRTV,Krazy Kanucks,Seal Team India - Core,Operator Portal",
"sprintName":"MF-S96",
"director":"Rajesh",
"locations":"Ukraine,SC,Duluth,India,Montreal,LA",
"countries":"U.S.,Ukraine,Canada,Serbia,Spain,India",
"cities":"Santa Clara,Ontario Quebec,Duluth,Bangalore,L.A.",
"vendors":"EPAM,FTE,ITC,L&T TS,G&L,Phalita,Assistera",
"overallSayDo":15.14,
"commitedSayDo":15,
"extraSayDo":0.93
},
{
"teamName":"EPAM Backend 1,Cloud Engineering,Content Infrastructure Montreal,Jedi,Seal Team India - APIs,Seal Team India - Security,Seal Team X,Seal Team X India,Sith,UC Live,WeRTV,Krazy Kanucks,Seal Team India - Core,Operator Portal",
"sprintName":"MF-S95",
"director":"Rajesh",
"locations":"Ukraine,SC,Duluth,India,Montreal,LA",
"countries":"U.S.,Ukraine,Canada,Serbia,Spain,India",
"cities":"Santa Clara,Ontario Quebec,Duluth,Bangalore,L.A.",
"vendors":"EPAM,FTE,ITC,L&T TS,G&L,Phalita,Assistera",
"overallSayDo":66.64,
"commitedSayDo":62,
"extraSayDo":19.79
},
{
"teamName":"EPAM Backend 1,Cloud Engineering,Content Infrastructure Montreal,Jedi,Seal Team India - APIs,Seal Team India - Security,Seal Team X,Seal Team X India,Sith,UC Live,WeRTV,Krazy Kanucks,Seal Team India - Core,Operator Portal",
"sprintName":"MF-S94",
"director":"Rajesh",
"locations":"Ukraine,SC,Duluth,India,Montreal,LA",
"countries":"U.S.,Ukraine,Canada,Serbia,Spain,India",
"cities":"Santa Clara,Ontario Quebec,Duluth,Bangalore,L.A.",
"vendors":"EPAM,FTE,ITC,L&T TS,G&L,Phalita,Assistera",
"overallSayDo":55.57,
"commitedSayDo":50.36,
"extraSayDo":35.29
}
]
我该如何修正append语句?
答案 0 :(得分:0)
是的,很明显,就像我们从错误中看到的那样。 只需将其更改为:
category_director_dataset.append("DIRECTOR+SPRINTNAME")
category_director_dataset.append({'overallSayDo': 'overallSayDo_value',
'commitedSayDo': 'commitedSayDo_value', 'extraSayDo': 'extraSayDo_value'})
答案 1 :(得分:0)
错误
可以在以下行中找到错误:
category_director_dataset.append("DIRECTOR+SPRINTNAME", {'overallSayDo': 'overallSayDo_value', 'commitedSayDo': 'commitedSayDo_value', 'extraSayDo': 'extraSayDo_value'})
^
^ error is this comma here
基本上可以简化为:
a_list.append(value1, value2)
仔细阅读错误消息:
TypeError:append()仅接受一个参数(给定2个参数)
append(x)
方法仅接受一个参数,但您已经在其中添加了两个参数。
您可以做什么
DIRECTOR+SPRINTNAME
作为键?)带有字典
DIRECTOR = "director"
SPRINTNAME = "sprint"
category_director_dataset = {}
category_director_dataset[DIRECTOR + SPRINTNAME] = { ... your data here ... }
因此,如果您要检索给定DIRECTOR + SPRINTNAME
的JSON,只需执行以下操作:
json = category_director_dataset[DIRECTOR + SPRINTNAME]