2015-05-23 24 views
0

我有一个解析好的excel文件,并且具有JSON风格的数据库,如下所示。在python中循环并创建JSON文件的函数不起作用

excel=[{'country': u'CH', 'product': u'MDF','scenario': u'BAU','year': 2010}, 
{'country': u'CH', 'product': u'OSB', 'scenario': u'BAU','year': 2010}, 
{'country': u'CH','product': u'MDF','scenario': u'BAU', 'year': 2011}, 
{'country': u'CH', 'product': u'OSB', 'scenario': u'BAU','year': 2011}, 
{'country': u'IT', 'product': u'MDF','scenario': u'BAU','year': 2010}, 
{'country': u'IT', 'product': u'OSB', 'scenario': u'BAU','year': 2010}, 
{'country': u'IT','product': u'MDF','scenario': u'BAU', 'year': 2011}, 
{'country': u'IT', 'product': u'OSB', 'scenario': u'BAU','year': 2011}] 

my_JSON={ 
    ('PEM_FORMIT', u'medium density fibreboard, at plant'): { 
     'PEM_code': 'MDF', 
     u'code': 245}, 
    ('PEM_FORMIT', u'oriented strand board, at plant'): { 
     'PEM_code': 'OSB', 
     u'code': 245}} 

基本上我想创建另一个JSON,每当在Excel的“产品”是一样的“PEM代码”创建另一个JSON,添加一个名字,然后复制“my_json”相对的对象,并添加等领域,就像下面的'excel year'和'excel country'一样。 在这个例子中应该创建一个8而不是2对象的JSON。

我写的函数创建了8个具有正确名称的JSON对象,但它混淆了所有其他额外的字段添加(即在这种情况下'excel国家'和'年份'),但一个'excel产品' 。 你能帮我理解为什么吗?

def myfunc(excel,my_JSON): 
    empty_dict={} 
    for rows in excel: 
     if rows['scenario']=='BAU': 
      for key,values in my_JSON.iteritems(): 
       if rows['product']==values['PEM_code']: 
        name_db="FORMIT_PEM_{}".format(rows['scenario']),"{} {} {}".format(rows['country'],rows['year'],rows['product']) 
        if name_db not in empty_dict: 
         empty_dict[name_db]=values 
         empty_dict[name_db]['excel product']=rows['product'], 
         empty_dict[name_db]['excel year']=rows['year'] 
         empty_dict[name_db]['excel country']=rows['country'] 



    return empty_dict 

返回这是错误

{('FORMIT_PEM_BAU', 'CH 2010 MDF'): {'PEM_code': 'MDF', 
    u'code': 245, 
    'excel country': u'IT', # should be CH 
    'excel product': (u'MDF',), 
    'excel year': 2011},  #should be 2010 
('FORMIT_PEM_BAU', 'CH 2010 OSB'): {'PEM_code': 'OSB', 
    u'code': 245, 
    'excel country': u'IT', # should be CH 
    'excel product': (u'OSB',), 
    'excel year': 2011},  # should be 2010 
('FORMIT_PEM_BAU', 'CH 2011 MDF'): {'PEM_code': 'MDF', 
    u'code': 245, 
    'excel country': u'IT', # should be CH 
    'excel product': (u'MDF',), 
    'excel year': 2011}, 
('FORMIT_PEM_BAU', 'CH 2011 OSB'): {'PEM_code': 'OSB', 
    u'code': 245, 
    'excel country': u'IT', # should be CH 
    'excel product': (u'OSB',), 
    'excel year': 2011}, 
    ............. 

哪里错误? 谢谢!

+0

嗨,大家好,很抱歉,我在这里张贴的新手,没有太多的写'好'帖子的经验。你能告诉我什么是错误的,我应该改进以避免将来出现同样的问题? Thanks ahead –

回答

0

您更改了my_json结构的字典,因此您以后更改了empty_dict的内容。相反,你应该复制一份:

def myfunc(excel,my_JSON): 
    empty_dict={} 
    for rows in excel: 
     if rows['scenario']=='BAU': 
      for key,values in my_JSON.iteritems(): 
       if rows['product']==values['PEM_code']: 
        name_db="FORMIT_PEM_{}".format(rows['scenario']),"{} {} {}".format(rows['country'],rows['year'],rows['product']) 
        if name_db not in empty_dict: 
         empty_dict[name_db] = dict(values) # copy 
         empty_dict[name_db]['excel product']=rows['product'], 
         empty_dict[name_db]['excel year']=rows['year'] 
         empty_dict[name_db]['excel country']=rows['country'] 



    return empty_dict 
+0

感谢丹尼尔它的工作原理! –

+0

@GiuseppeCardellini:然后将其标记为已接受的答案。 – Daniel