我有一些内部词典,我的逻辑有点偏离,因为我的输出接近我想要完成的但尚未完成。我想从内部字典中连接两个变量的值。
在循环中我的变量打印正确,但当我使用另一个变量来连接两者时,我只收到一些值。此数据来自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
答案 0 :(得分: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'
请注意,我选择在打印前将所有内容存储在字符串中。没有必要,但它是一种直接的方式来获得所需的输出。