I'm trying to parse the value of some categories
according to their category name
from a dictionary. The category names I've chosen to parse from this dictionary are Interior Features
and Spaces and Amenities
for now. The follwing script can parse them but when I tried to print them, the alignments of that output become messy which obviously impacts similarly in a csv file when I write them.
How can I print the result and write the same in a csv file in the right way?
Here are the content for your consideration:
[{'categoryGroupName': 'Interior Features', 'categories': [{'categoryName': 'Bedrooms', 'categoryFacts': [{'factLabel': 'Beds', 'factValue': '3'}, {'factLabel': 'Master bedroom location', 'factValue': 'Main'}]}, {'categoryName': 'Kitchen', 'categoryFacts': [{'factLabel': 'Kitchen location', 'factValue': 'Main'}]}, {'categoryName': 'Other Rooms', 'categoryFacts': [{'factLabel': 'Dining room location', 'factValue': 'Main'}, {'factLabel': 'Living room location', 'factValue': 'Main'}, {'factLabel': 'Utility room location', 'factValue': 'Lower'}]}, {'categoryName': 'Heating and Cooling', 'categoryFacts': [{'factLabel': 'Heating', 'factValue': 'Forced air'}, {'factLabel': 'Heating', 'factValue': 'Gas'}, {'factLabel': 'Heating and cooling', 'factValue': '90%+ High Efficiency, Forced Air'}]}, {'categoryName': 'Basement', 'categoryFacts': [{'factLabel': None, 'factValue': 'Partially finished'}, {'factLabel': None, 'factValue': '900 sqft basement'}, {'factLabel': 'Basement', 'factValue': 'Partially Finished'}]}, {'categoryName': 'Attic', 'categoryFacts': [{'factLabel': None, 'factValue': 'Attic'}]}, {'categoryName': 'Appliances', 'categoryFacts': [{'factLabel': 'Appliances included', 'factValue': 'Dishwasher, Dryer, Microwave, Range / Oven, Refrigerator, Washer'}, {'factLabel': 'Appliances that stay', 'factValue': 'Range/Oven, Dishwasher, Refrigerator, Washer, Dryer'}]}, {'categoryName': 'Flooring', 'categoryFacts': [{'factLabel': 'Floor size', 'factValue': '2,070 sqft'}, {'factLabel': 'Flooring', 'factValue': 'Carpet, Hardwood, Tile'}, {'factLabel': 'Floor covering', 'factValue': 'Hardwood, Laminate, Wall to Wall Carpet'}]}, {'categoryName': 'Other Interior Features', 'categoryFacts': [{'factLabel': 'Addition size', 'factValue': '900'}, {'factLabel': None, 'factValue': 'Fireplace'}, {'factLabel': None, 'factValue': 'Ceiling Fan'}, {'factLabel': 'Features', 'factValue': 'Dbl Pane/Storm Windw, Dining Room, Security System, Ceiling Fan(s), Hot Tub/Spa'}]}]}, {'categoryGroupName': 'Building', 'categories': [{'categoryName': 'Other Building Features', 'categoryFacts': [{'factLabel': 'Building info', 'factValue': 'Built On Lot'}]}]}, {'categoryGroupName': 'Spaces and Amenities', 'categories': [{'categoryName': 'Size', 'categoryFacts': [{'factLabel': 'Unit count', 'factValue': '1'}]}, {'categoryName': 'Spaces', 'categoryFacts': [{'factLabel': None, 'factValue': 'Hot Tub/Spa'}]}, {'categoryName': 'Amenities', 'categoryFacts': [{'factLabel': None, 'factValue': 'Security System'}]}]}, {'categoryGroupName': 'Construction', 'categories': [{'categoryName': 'Type and Style', 'categoryFacts': [{'factLabel': 'Structure type', 'factValue': 'Craftsman'}, {'factLabel': None, 'factValue': 'Single Family'}, {'factLabel': 'Architecture', 'factValue': 'Craftsman'}, {'factLabel': 'Entrance level', 'factValue': 'Main'}, {'factLabel': 'Style', 'factValue': '16 - 1 Story w/Bsmnt.'}]}, {'categoryName': 'Materials', 'categoryFacts': [{'factLabel': 'Roof type', 'factValue': 'Composition'}, {'factLabel': 'Exterior material', 'factValue': 'Wood'}, {'factLabel': 'Roof', 'factValue': 'Composition'}, {'factLabel': 'Foundation', 'factValue': 'Poured Concrete'}]}, {'categoryName': 'Dates', 'categoryFacts': [{'factLabel': None, 'factValue': 'Built in 1916'}]}, {'categoryName': 'Other Construction Features', 'categoryFacts': [{'factLabel': 'Stories', 'factValue': '1'}]}]}, {'categoryGroupName': 'Exterior Features', 'categories': [{'categoryName': 'Patio', 'categoryFacts': [{'factLabel': None, 'factValue': 'Deck'}]}, {'categoryName': 'Yard', 'categoryFacts': [{'factLabel': None, 'factValue': 'Fenced Yard'}, {'factLabel': None, 'factValue': 'Garden'}]}, {'categoryName': 'View Type', 'categoryFacts': [{'factLabel': 'View', 'factValue': 'Mountain'}, {'factLabel': 'View', 'factValue': 'Territorial, See Remarks'}]}, {'categoryName': 'Lot', 'categoryFacts': [{'factLabel': 'Lot', 'factValue': '4,000 sqft'}, {'factLabel': 'Lot topography/vegetation', 'factValue': 'Level, Steep Slope'}, {'factLabel': 'Lot details', 'factValue': 'Paved Street, Sidewalk, Curbs'}]}, {'categoryName': 'Other Exterior Features', 'categoryFacts': [{'factLabel': 'Parcel #', 'factValue': '6610001005'}, {'factLabel': 'Exterior', 'factValue': 'Wood'}]}]}, {'categoryGroupName': 'Community and Neighborhood', 'categories': [{'categoryName': 'Schools', 'categoryFacts': [{'factLabel': 'Elementary school', 'factValue': 'Buyer To Verify'}, {'factLabel': 'Middle school', 'factValue': 'Buyer To Verify'}, {'factLabel': 'High school', 'factValue': 'Buyer To Verify'}, {'factLabel': 'School district', 'factValue': 'Seattle'}]}, {'categoryName': 'Transportation', 'categoryFacts': [{'factLabel': 'Bus line nearby', 'factValue': 'Y'}]}]}, {'categoryGroupName': 'Parking', 'categories': [{'categoryName': '', 'categoryFacts': [{'factLabel': 'Parking', 'factValue': 'Attached Garage, 1 space, 290 sqft garage'}, {'factLabel': 'Parking type', 'factValue': 'Garage-Attached, Off Street'}]}]}, {'categoryGroupName': 'Utilities', 'categories': [{'categoryName': 'Utilities', 'categoryFacts': [{'factLabel': None, 'factValue': 'Cable Ready'}, {'factLabel': 'Sewer', 'factValue': 'Sewer Connected'}, {'factLabel': 'Water source', 'factValue': 'Public'}]}, {'categoryName': 'Green Energy', 'categoryFacts': [{'factLabel': 'Energy source', 'factValue': 'Natural Gas, Solar (Unspecified)'}]}]}, {'categoryGroupName': 'Other', 'categories': [{'categoryName': '', 'categoryFacts': [{'factLabel': 'Last sold', 'factValue': 'Dec 2019 for $900,000'}, {'factLabel': 'Last sale price/sqft', 'factValue': '$435'}, {'factLabel': 'Property type', 'factValue': 'RESI'}, {'factLabel': 'Possesion', 'factValue': 'Closing'}, {'factLabel': 'Form 17', 'factValue': 'Provided'}, {'factLabel': 'Potential terms', 'factValue': 'Cash Out, Conventional'}, {'factLabel': 'Site features', 'factValue': 'Cable TV, Deck, Fenced-Partially, Gas Available, Hot Tub/Spa'}, {'factLabel': 'Offers', 'factValue': 'Reviewed on receipt'}, {'factLabel': 'Commission', 'factValue': '2.5%'}]}]}, {'categoryGroupName': 'Activity On Zillow', 'categories': [{'categoryName': '', 'categoryFacts': [{'factLabel': 'Views since listing', 'factValue': '77'}, {'factLabel': None, 'factValue': '1 shopper saved this home'}]}]}]
Script I've tried with:
desc = [{'categoryGroupName': 'Interior Features', 'categories': [{'categoryName': 'Bedrooms', 'categoryFacts': [{'factLabel': 'Beds', 'factValue': '3'}, {'factLabel': 'Master bedroom location', 'factValue': 'Main'}]}, {'categoryName': 'Kitchen', 'categoryFacts': [{'factLabel': 'Kitchen location', 'factValue': 'Main'}]}, {'categoryName': 'Other Rooms', 'categoryFacts': [{'factLabel': 'Dining room location', 'factValue': 'Main'}, {'factLabel': 'Living room location', 'factValue': 'Main'}, {'factLabel': 'Utility room location', 'factValue': 'Lower'}]}, {'categoryName': 'Heating and Cooling', 'categoryFacts': [{'factLabel': 'Heating', 'factValue': 'Forced air'}, {'factLabel': 'Heating', 'factValue': 'Gas'}, {'factLabel': 'Heating and cooling', 'factValue': '90%+ High Efficiency, Forced Air'}]}, {'categoryName': 'Basement', 'categoryFacts': [{'factLabel': None, 'factValue': 'Partially finished'}, {'factLabel': None, 'factValue': '900 sqft basement'}, {'factLabel': 'Basement', 'factValue': 'Partially Finished'}]}, {'categoryName': 'Attic', 'categoryFacts': [{'factLabel': None, 'factValue': 'Attic'}]}, {'categoryName': 'Appliances', 'categoryFacts': [{'factLabel': 'Appliances included', 'factValue': 'Dishwasher, Dryer, Microwave, Range / Oven, Refrigerator, Washer'}, {'factLabel': 'Appliances that stay', 'factValue': 'Range/Oven, Dishwasher, Refrigerator, Washer, Dryer'}]}, {'categoryName': 'Flooring', 'categoryFacts': [{'factLabel': 'Floor size', 'factValue': '2,070 sqft'}, {'factLabel': 'Flooring', 'factValue': 'Carpet, Hardwood, Tile'}, {'factLabel': 'Floor covering', 'factValue': 'Hardwood, Laminate, Wall to Wall Carpet'}]}, {'categoryName': 'Other Interior Features', 'categoryFacts': [{'factLabel': 'Addition size', 'factValue': '900'}, {'factLabel': None, 'factValue': 'Fireplace'}, {'factLabel': None, 'factValue': 'Ceiling Fan'}, {'factLabel': 'Features', 'factValue': 'Dbl Pane/Storm Windw, Dining Room, Security System, Ceiling Fan(s), Hot Tub/Spa'}]}]}, {'categoryGroupName': 'Building', 'categories': [{'categoryName': 'Other Building Features', 'categoryFacts': [{'factLabel': 'Building info', 'factValue': 'Built On Lot'}]}]}, {'categoryGroupName': 'Spaces and Amenities', 'categories': [{'categoryName': 'Size', 'categoryFacts': [{'factLabel': 'Unit count', 'factValue': '1'}]}, {'categoryName': 'Spaces', 'categoryFacts': [{'factLabel': None, 'factValue': 'Hot Tub/Spa'}]}, {'categoryName': 'Amenities', 'categoryFacts': [{'factLabel': None, 'factValue': 'Security System'}]}]}, {'categoryGroupName': 'Construction', 'categories': [{'categoryName': 'Type and Style', 'categoryFacts': [{'factLabel': 'Structure type', 'factValue': 'Craftsman'}, {'factLabel': None, 'factValue': 'Single Family'}, {'factLabel': 'Architecture', 'factValue': 'Craftsman'}, {'factLabel': 'Entrance level', 'factValue': 'Main'}, {'factLabel': 'Style', 'factValue': '16 - 1 Story w/Bsmnt.'}]}, {'categoryName': 'Materials', 'categoryFacts': [{'factLabel': 'Roof type', 'factValue': 'Composition'}, {'factLabel': 'Exterior material', 'factValue': 'Wood'}, {'factLabel': 'Roof', 'factValue': 'Composition'}, {'factLabel': 'Foundation', 'factValue': 'Poured Concrete'}]}, {'categoryName': 'Dates', 'categoryFacts': [{'factLabel': None, 'factValue': 'Built in 1916'}]}, {'categoryName': 'Other Construction Features', 'categoryFacts': [{'factLabel': 'Stories', 'factValue': '1'}]}]}, {'categoryGroupName': 'Exterior Features', 'categories': [{'categoryName': 'Patio', 'categoryFacts': [{'factLabel': None, 'factValue': 'Deck'}]}, {'categoryName': 'Yard', 'categoryFacts': [{'factLabel': None, 'factValue': 'Fenced Yard'}, {'factLabel': None, 'factValue': 'Garden'}]}, {'categoryName': 'View Type', 'categoryFacts': [{'factLabel': 'View', 'factValue': 'Mountain'}, {'factLabel': 'View', 'factValue': 'Territorial, See Remarks'}]}, {'categoryName': 'Lot', 'categoryFacts': [{'factLabel': 'Lot', 'factValue': '4,000 sqft'}, {'factLabel': 'Lot topography/vegetation', 'factValue': 'Level, Steep Slope'}, {'factLabel': 'Lot details', 'factValue': 'Paved Street, Sidewalk, Curbs'}]}, {'categoryName': 'Other Exterior Features', 'categoryFacts': [{'factLabel': 'Parcel #', 'factValue': '6610001005'}, {'factLabel': 'Exterior', 'factValue': 'Wood'}]}]}, {'categoryGroupName': 'Community and Neighborhood', 'categories': [{'categoryName': 'Schools', 'categoryFacts': [{'factLabel': 'Elementary school', 'factValue': 'Buyer To Verify'}, {'factLabel': 'Middle school', 'factValue': 'Buyer To Verify'}, {'factLabel': 'High school', 'factValue': 'Buyer To Verify'}, {'factLabel': 'School district', 'factValue': 'Seattle'}]}, {'categoryName': 'Transportation', 'categoryFacts': [{'factLabel': 'Bus line nearby', 'factValue': 'Y'}]}]}, {'categoryGroupName': 'Parking', 'categories': [{'categoryName': '', 'categoryFacts': [{'factLabel': 'Parking', 'factValue': 'Attached Garage, 1 space, 290 sqft garage'}, {'factLabel': 'Parking type', 'factValue': 'Garage-Attached, Off Street'}]}]}, {'categoryGroupName': 'Utilities', 'categories': [{'categoryName': 'Utilities', 'categoryFacts': [{'factLabel': None, 'factValue': 'Cable Ready'}, {'factLabel': 'Sewer', 'factValue': 'Sewer Connected'}, {'factLabel': 'Water source', 'factValue': 'Public'}]}, {'categoryName': 'Green Energy', 'categoryFacts': [{'factLabel': 'Energy source', 'factValue': 'Natural Gas, Solar (Unspecified)'}]}]}, {'categoryGroupName': 'Other', 'categories': [{'categoryName': '', 'categoryFacts': [{'factLabel': 'Last sold', 'factValue': 'Dec 2019 for $900,000'}, {'factLabel': 'Last sale price/sqft', 'factValue': '$435'}, {'factLabel': 'Property type', 'factValue': 'RESI'}, {'factLabel': 'Possesion', 'factValue': 'Closing'}, {'factLabel': 'Form 17', 'factValue': 'Provided'}, {'factLabel': 'Potential terms', 'factValue': 'Cash Out, Conventional'}, {'factLabel': 'Site features', 'factValue': 'Cable TV, Deck, Fenced-Partially, Gas Available, Hot Tub/Spa'}, {'factLabel': 'Offers', 'factValue': 'Reviewed on receipt'}, {'factLabel': 'Commission', 'factValue': '2.5%'}]}]}, {'categoryGroupName': 'Activity On Zillow', 'categories': [{'categoryName': '', 'categoryFacts': [{'factLabel': 'Views since listing', 'factValue': '77'}, {'factLabel': None, 'factValue': '1 shopper saved this home'}]}]}]
name = "property in somewhere"
for elem in desc:
if elem['categoryGroupName']=='Interior Features':
interior_features = {curr['factLabel']: curr['factValue'] for i in elem['categories'] for curr in i['categoryFacts']}
interior_features = str(interior_features).replace("{","").replace("}","").replace("'","")
else:
interior_features = ""
if elem['categoryGroupName']=='Spaces and Amenities':
space_amenities = {curr['factLabel']: curr['factValue'] for i in elem['categories'] for curr in i['categoryFacts']}
space_amenities = str(space_amenities).replace("{","").replace("}","").replace("'","")
else:
space_amenities = ""
print(name,interior_features,space_amenities)
Result I'm getting:
property in somewhere Beds: 3, Master bedroom location: Main, Kitchen location: Main, Dining room location: Main, Living room location: Main, Utility room location: Lower, Heating: Gas, Heating and cooling: 90%+ High Efficiency, Forced Air, None: Ceiling Fan, Basement: Partially Finished, Appliances included: Dishwasher, Dryer, Microwave, Range / Oven, Refrigerator, Washer, Appliances that stay: Range/Oven, Dishwasher, Refrigerator, Washer, Dryer, Floor size: 2,070 sqft, Flooring: Carpet, Hardwood, Tile, Floor covering: Hardwood, Laminate, Wall to Wall Carpet, Addition size: 900, Features: Dbl Pane/Storm Windw, Dining Room, Security System, Ceiling Fan(s), Hot Tub/Spa
property in somewhere
property in somewhere Unit count: 1, None: Security System
property in somewhere
property in somewhere
property in somewhere
property in somewhere
property in somewhere
property in somewhere
property in somewhere
In the above output you can see that this line property in somewhere
has been printed several times whereas it should be printed once. This line Unit count: 1, None: Security System
should be in coulmn three whereas it has got placed some unwanted area.
The bottom line is the above script should print the results once but it prints multiple times because of the for loop.
Expected output (pasted here the variable names instead of their values to make you understand the alignment). To be more clear - there is no result for row b, row c and so on. The result should be printed once and limited to Row A1, Row B1 and Row C1. Period.
Column A Column B Column C
name interior_features space_amenities