Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法相应地打印结果并将相同的结果写入csv文件_Python_Json_Python 3.x_Csv_Dictionary - Fatal编程技术网

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

Python 无法相应地打印结果并将相同的结果写入csv文件,python,json,python-3.x,csv,dictionary,Python,Json,Python 3.x,Csv,Dictionary,我试图根据字典中的类别名称来解析一些类别的值。我从这本词典中选择要解析的类别名称是内部功能和空间和便利设施。下面的脚本可以解析它们,但当我试图打印它们时,输出的对齐变得混乱,这显然会在我编写它们时对csv文件产生类似的影响 如何以正确的方式打印结果并将其写入csv文件 以下是供您考虑的内容: [{'categoryGroupName': 'Interior Features', 'categories': [{'categoryName': 'Bedrooms', 'categoryFacts'

我试图根据字典中的
类别名称来解析一些
类别的值。我从这本词典中选择要解析的类别名称是
内部功能
空间和便利设施
。下面的脚本可以解析它们,但当我试图打印它们时,输出的对齐变得混乱,这显然会在我编写它们时对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
在上面的输出中,您可以看到,某处的这一行
属性已打印多次,而它应该打印一次。这一行<代码>单位计数:1,无:安全系统应该在第三列,而它已经放置了一些不需要的区域

底线是上面的脚本应该打印一次结果,但由于for循环,它会打印多次

预期输出(将变量名粘贴在此处,而不是将其值粘贴在此处,以使您了解对齐方式)。更清楚的是,b行、c行等没有结果。结果应打印一次,并限于第A1行、第B1行和第C1行。句号

Column A    Column B             Column C
name        interior_features    space_amenities
代码的两个mod:

(1) 根据每列的最大宽度左对齐每个字段

(2) 不要打印与所需类别不匹配的类别(即返回empy功能的类别)

输出

property in somewhere   interior_features                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   space_amenities                       
property in somewhere   Appliances included: Dishwasher, Dryer, Microwave, Range / Oven, Refrigerator, Washer, Appliances that stay: Range/Oven, Dishwasher, Refrigerator, Washer, Dryer, Master bedroom location: Main, Dining room location: Main, Heating and cooling: 90%+ High Efficiency, Forced Air, Features: Dbl Pane/Storm Windw, Dining Room, Security System, Ceiling Fan(s), Hot Tub/Spa, Heating: Gas, Floor size: 2,070 sqft, Utility room location: Lower, Living room location: Main, Addition size: 900, Beds: 3, Basement: Partially Finished, Flooring: Carpet, Hardwood, Tile, None: Ceiling Fan, Floor covering: Hardwood, Laminate, Wall to Wall Carpet, Kitchen location: Main                                            
property in somewhere                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       None: Security System, Unit count: 1  

如果我理解正确,目标是从
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)
但是,如果某个值出现多次,则上述代码可能存在问题,因为它只存储最后一个值。在这种情况下,我们可以使用空列表作为
字典的值。然后附加在
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)
如果要将
字典转换为列表,可以使用
行.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)

你肯定误解了我所期望的结果。查看编辑csv输出。我从来没有说过我解析字段的方式是实现这一点的理想方式。如果出现更好的逻辑,解析逻辑总是可以被纠正以去除那些制表符和空格。谢谢.@robots.txt--谢谢您的澄清。我相应地修改了我的答案。@robots.txt--误解是您将其称为CSV文件,通常不需要对齐文本进行显示(例如,可以使用选项卡来标识每个字段)。我在字段之间放置了一个选项卡,因此尽管有额外的空间使其列对齐,但仍然可以将其作为CSV处理。@robots.txt--多次打印“某处的属性”的原因是您有10个类别,即:[‘内部特征’、‘建筑’、‘空间和设施’、‘建筑’、‘外部特征’、‘社区和邻里’、‘停车’、‘公用设施’、‘其他’、‘Zillow上的活动’]。其中只有两个与您的筛选器匹配并具有数据,而其他的则为内饰和空间设施打印空字符串。嘿,robots.txt,很高兴再次见到您。如果我理解正确,每个
desc
列表都应该有一个
内饰功能
项和一个
空间设施
项。如果是这样,您可以创建一个包含
名称
值的列表,当您找到
内部设施
空间设施
值时,将其添加到列表中。循环结束时,您应该有一个包含三项的列表:
[名称、内部设施、空间设施]
您可以打印或保存到文件中。如果有不清楚的地方,或者我误解了问题,请告诉我。您好@t.m.adam,很高兴在循环中找到您。如果您检查,您可以发现我的纠正方法符合您的建议。请通读。谢谢。好的,在这种情况下,您可以使用默认为N/a的词典值(例如:
rows={'name':name,'interior_features':'N/A','space_facilities':'N/A','virtual_stuff':'N/A'}
)。当您找到一个项时,将其添加到字典中;如果没有项,字典键将具有默认值-N/A。您可以使用
dict.values()
在列表中获取字典值。这个想法似乎很有希望@t.m.adam。问题是我真的找不到任何关于如何在脚本中实现它的想法,因为我在处理字典内容方面远远落后。最后,我能够说服你用python问题写一个答案。保持冷静,直到下一个麻烦。哈!祝2020年万事如意!谢谢。忘了问一个非常不相关的问题。你有选择深入研究scrapy吗?没有,我根本不使用scrapy。现在,我也有一个问题。一个值是否可能在
desc
列表中出现多次?如果有,我的代码可能有问题,因为
rows
键将仅具有最后一个值。
import csv

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