gpt4 book ai didi

python - 无法相应地打印结果并将相同的结果写入 csv 文件

转载 作者:行者123 更新时间:2023-12-01 00:13:00 26 4
gpt4 key购买 nike

我正在尝试根据字典中的类别名称来解析某些类别的值。目前,我选择从该字典中解析的类别名称是 Interior FeaturesSpaces and Facilities。下面的脚本可以解析它们,但是当我尝试打印它们时,输出的对齐方式变得困惑,当我编写它们时,这显然会在 csv 文件中产生类似的影响。

如何打印结果并以正确的方式将其写入 csv 文件?

以下内容供您考虑:

[{'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'}]}]}]

我尝试过的脚本:

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)

我得到的结果:

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

在上面的输出中,您可以看到这一行property in Something已被打印多次,而它应该打印一次。此行Unit count: 1, None: Security System 应该位于第三列,但它已放置在一些不需要的区域。

The bottom line is the above script should print the results once but it prints multiple times because of the for loop.

预期输出(在此处粘贴变量名称而不是它们的值,以便您了解对齐方式)。更清楚地说,b 行、c 行等没有结果。结果应打印一次,并仅限于 A1 行、B1 行和 C1 行。期间。

Column A    Column B             Column C
name interior_features space_amenities

最佳答案

如果我理解正确的话,目标是从 desc 中的项目中获取某些值。每个值可能出现一次或根本不出现 - 在这种情况下,结果应为“N/A”。因此,您可以使用具有 N/A 默认值的字典,然后将其替换为 desc 列表中的值。

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"
rows = {'name': name, 'interior_features':'N/A', 'space_amenities':'N/A', 'imaginary_stuff':'N/A'}

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("'","")
rows['interior_features'] = interior_features

if elem['categoryGroupName']=='Imaginary Stuff':
imaginary_stuff = {curr['factLabel']: curr['factValue'] for i in elem['categories'] for curr in i['categoryFacts']}
imaginary_stuff = str(imaginary_stuff).replace("{","").replace("}","").replace("'","")
rows['imaginary_stuff'] = imaginary_stuff

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("'","")
rows['space_amenities'] = space_amenities

print(rows)

但是,如果一个值出现多次,上面的代码可能会出现问题,因为它只会存储最后一个值。在这种情况下,我们可以使用空列表作为 rows 字典的值。然后附加在 desc 列表中找到的值。最后,如果列表有值,则将它们连接到字符串中,如果没有,则将空列表替换为“N/A”。

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"
rows = {'name': name, 'interior_features':[], 'space_amenities':[], 'imaginary_stuff':[]}

for elem in desc:
if elem['categoryGroupName']=='Interior Features' or elem['categoryGroupName']== 'Spaces and Amenities':
interior_features = {curr['factLabel']: curr['factValue'] for i in elem['categories'] for curr in i['categoryFacts']}
interior_features = str(interior_features).replace("{","").replace("}","").replace("'","")
rows['interior_features'].append(interior_features)

if elem['categoryGroupName']=='Imaginary Stuff':
imaginary_stuff = {curr['factLabel']: curr['factValue'] for i in elem['categories'] for curr in i['categoryFacts']}
imaginary_stuff = str(imaginary_stuff).replace("{","").replace("}","").replace("'","")
rows['imaginary_stuff'].append(imaginary_stuff)

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("'","")
rows['space_amenities'].append(space_amenities)


separator = ' | '
rows = {k: separator.join(v) if v else 'N/A' for k,v in rows.items()}
print(rows)

如果您希望将rows字典转换为列表,可以使用rows.values()。您还可以使用 csv.DictWriter() 以便将结果写入文件,而无需将其转换为列表。

import csv

with open('file.csv', 'w', encoding='utf-8', newline='') as f:
w = csv.DictWriter(f, rows.keys())
w.writeheader()
w.writerow(rows)

关于python - 无法相应地打印结果并将相同的结果写入 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59500206/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com