Python-附加值

时间:2018-09-04 12:12:25

标签: python json python-2.7

我正在从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语句?

2 个答案:

答案 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)方法仅接受一个参数,但您已经在其中添加了两个参数。

您可以做什么

  1. 将您的参数放入一个元组(用括号括起来)以将参数数量减少为一个
  2. 使用其他数据结构(也许字典对您的情况有用,因为您已经提到过要使用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]