2015-06-17 50 views
-1

我有很多內部字典,我的邏輯有點偏離,因爲我的輸出接近我想完成但尚未完成的任務。我想連接來自內部詞典的兩個變量的值。使用if語句循環嵌套字典Python

雖然在循環中我的變量正確打印,但是當我使用另一個變量來連接兩個時,我只收到一些值。這些數據來自JSON結構。

代碼:

try: 
    k_bulky_count = ' ' 
    for sr in BulkyItem: 
      for bulkycount in BulkyItem['BulkyItem']: 
          countBulky = bulkycount['BulkyItemCount'] 
          if k_bulky_count != countBulky: 
           k_bulky_count = countBulky 
           print k_bulky_count 
except: 
    print "data" 

try: 
    k_bulky_item = '' 
    for sr in BulkyItem: 
     for bulkyitem in BulkyItem['BulkyItem']: 
      itemBulky = bulkyitem['BulkyItemType'] 
      if k_bulky_item != itemBulky: 
        k_bulky_item = itemBulky 
        BulkyItemInfo += '{0}, {1} '.format (k_bulky_count, k_bulky_item) 
        print BulkyItemInfo 
except: 
    print ("No Bulky Info") 

輸出爲BulkyItemInfo:

1, Carpet 
1, Carpet 1, Chair 
1, Carpet 1, Chair 1, Desk 
1, Carpet 1, Chair 1, Desk 1, Wood Bundles 
1, Carpet 1, Chair 1, Desk 1, Wood Bundles 1, Other 
1, Bookcase 
1, Bicycle 
2, Carpet 
2, Carpet 2, Chair 
2, Carpet 2, Chair 2, Headboard 
1, Bicycle 
1, Bicycle 1, Bird Cage (Plastic) 
1, Bicycle 1, Bird Cage (Plastic) 1, Blinds 
1, Bicycle 1, Bird Cage (Plastic) 1, Blinds 1, Bookcase 
1, Bicycle 1, Bird Cage (Plastic) 1, Blinds 1, Bookcase 1, Dresser 
1, Bicycle 1, Bird Cage (Plastic) 1, Blinds 1, Bookcase 1, Dresser 1, Entertainment Center 
1, Bookcase 
1, Bookcase 1, Desk 
1, Bookcase 1, Desk 1, Shelf 

爲數量和類型輸出在各自環

打印時
1 
File Cabinet (Wood) 
1 
Carpet 
Chair 
Desk 
Wood Bundles 
Other 
1 
Bookcase 
1 
Bicycle 
5 
6 
2 
Carpet 
Chair 
Headboard 

的Json實施例:

{ 
    "ListOfLa311BulkyItem": { 
     "BulkyItem": [ 
      { 
       "BulkyItemCount": "5", 
       "BulkyItemType": "Carpet", 
       "CollectionLocation": "Curb", 
       "IllegallyDumped": "N", 
       "OtherBulkyItem": "", 
       "Type": "Bulky Items", 
       "SpecialRule": "Y", 
       "GatedCommunityMultifamilyDwelling": "", 
       "MobileHomeSpace": "", 
       "IllegalDumpCollectionLoc": "", 
       "LastUpdatedBy": "52299", 
       "DriverFirstName": "", 
       "DriverLastName": "", 
       "ServiceDateRendered": "", 
       "TruckNo": "", 
       "ActiveStatus": "Y", 
       "IllegalBulkyItemType": "", 
       "PurposeofSR": "", 
       "Name": "061720151151242471" 
      }, 
      { 
       "BulkyItemCount": "6", 
       "BulkyItemType": "Chair", 
       "CollectionLocation": "Curb", 
       "IllegallyDumped": "N", 
       "OtherBulkyItem": "", 
       "Type": "Bulky Items", 
       "SpecialRule": "", 
       "GatedCommunityMultifamilyDwelling": "", 
       "MobileHomeSpace": "", 
       "IllegalDumpCollectionLoc": "", 
       "LastUpdatedBy": "52299", 
       "DriverFirstName": "", 
       "DriverLastName": "", 
       "ServiceDateRendered": "", 
       "TruckNo": "", 
       "ActiveStatus": "Y", 
       "IllegalBulkyItemType": "", 
       "PurposeofSR": "", 
       "Name": "061720151151242472" 
      }, 
      { 
       "BulkyItemCount": "2", 
       "BulkyItemType": "Headboard", 
       "CollectionLocation": "Curb", 
       "IllegallyDumped": "N", 
       "OtherBulkyItem": "", 
       "Type": "Bulky Items", 
       "SpecialRule": "Y", 
       "GatedCommunityMultifamilyDwelling": "", 
       "MobileHomeSpace": "", 
       "IllegalDumpCollectionLoc": "", 
       "LastUpdatedBy": "52299", 
       "DriverFirstName": "", 
       "DriverLastName": "", 
       "ServiceDateRendered": "", 
       "TruckNo": "", 
       "ActiveStatus": "Y", 
       "IllegalBulkyItemType": "", 
       "PurposeofSR": "", 
       "Name": "061720151151242483" 
      } 
     ] 
    } 
} 
從上面的JSON

所需的輸出

5, Carpet, 6, Chair, 2 Headboard 
+4

爲什麼不使用標準庫的['json'](https://docs.python.org/3/library/json.html)模塊? – Olaf

+0

我認爲你完全誤解了這一點。 – geoffreyGIS

+0

原因可能是你沒有明確說明你想要達到什麼或者你的問題是什麼。所以,只是啓發我。 – Olaf

回答

0

我不知道你需要什麼,但是這個代碼提供從給定的JSON字符串所需的輸出:

import json 

jsonstr = ''' 
{ 
    "ListOfLa311BulkyItem": { 
     "BulkyItem": [ 
      { 
       "BulkyItemCount": "5", 
       "BulkyItemType": "Carpet", 
       "CollectionLocation": "Curb", 
       "IllegallyDumped": "N", 
       "OtherBulkyItem": "", 
       "Type": "Bulky Items", 
       "SpecialRule": "Y", 
       "GatedCommunityMultifamilyDwelling": "", 
       "MobileHomeSpace": "", 
       "IllegalDumpCollectionLoc": "", 
       "LastUpdatedBy": "52299", 
       "DriverFirstName": "", 
       "DriverLastName": "", 
       "ServiceDateRendered": "", 
       "TruckNo": "", 
       "ActiveStatus": "Y", 
       "IllegalBulkyItemType": "", 
       "PurposeofSR": "", 
       "Name": "061720151151242471" 
      }, 
      { 
       "BulkyItemCount": "6", 
       "BulkyItemType": "Chair", 
       "CollectionLocation": "Curb", 
       "IllegallyDumped": "N", 
       "OtherBulkyItem": "", 
       "Type": "Bulky Items", 
       "SpecialRule": "", 
       "GatedCommunityMultifamilyDwelling": "", 
       "MobileHomeSpace": "", 
       "IllegalDumpCollectionLoc": "", 
       "LastUpdatedBy": "52299", 
       "DriverFirstName": "", 
       "DriverLastName": "", 
       "ServiceDateRendered": "", 
       "TruckNo": "", 
       "ActiveStatus": "Y", 
       "IllegalBulkyItemType": "", 
       "PurposeofSR": "", 
       "Name": "061720151151242472" 
      }, 
      { 
       "BulkyItemCount": "2", 
       "BulkyItemType": "Headboard", 
       "CollectionLocation": "Curb", 
       "IllegallyDumped": "N", 
       "OtherBulkyItem": "", 
       "Type": "Bulky Items", 
       "SpecialRule": "Y", 
       "GatedCommunityMultifamilyDwelling": "", 
       "MobileHomeSpace": "", 
       "IllegalDumpCollectionLoc": "", 
       "LastUpdatedBy": "52299", 
       "DriverFirstName": "", 
       "DriverLastName": "", 
       "ServiceDateRendered": "", 
       "TruckNo": "", 
       "ActiveStatus": "Y", 
       "IllegalBulkyItemType": "", 
       "PurposeofSR": "", 
       "Name": "061720151151242483" 
      } 
     ] 
    } 
} 
''' 

dic = json.loads(jsonstr) 

astr = "" 
for listofbulkyitems in dic.itervalues(): 
    for bulkyitems in listofbulkyitems.itervalues(): 
     for bulkyitem in bulkyitems: 
      if astr: 
       astr += ", "  ## The string should not start by a comma but all the following items should be preceded by a comma 
      astr += bulkyitem['BulkyItemCount']+", "+bulkyitem['BulkyItemType'] 
print astr 
## prints '5, Carpet, 6, Chair, 2, Headboard' 

請注意,我選擇了存儲打印之前,一切都在一個字符串中。這不是必須的,但它是獲得理想輸出的直接方式。