我将把数据清理留给您,但是,这个整体方法应该可以帮助您实现您想要实现的目标。在
首先,您似乎只想要List键中的内容。因此,在您接收的数据结构中,只需获取List键中的数据:car_data = struct.get('List')
既然您有来自List的数据,并且假设该列表中的每个字典条目都有相同的“keys”,那么只需对字段名执行car_data[0].keys()。在
^{pr2}$
然后,只需使用这些字段名设置DictWriter,这一点很简单:struct = {'TotalCount': 1, 'PageSize': 1, 'FoundCategories': [], 'Page': 1, 'List': [{'AsAt': '/Date(1457733660023)/', 'Model': 'Continental', 'Suburb': 'Palmerston North', 'NoteDate': '/Date(0)/', 'PriceDisplay': '$100.00', 'EndDate': '/Date(1457930919847)/', 'RegistrationExpires': '/Date(0)/', 'StartPrice': 100.0, 'Owners': 0, 'ListingLength': None, 'CategoryPath': '/Trade-Me-Motors/Cars/Bentley', 'ListingId': 4550689, 'Subtitle': 'sdf', 'Category': '0001-0268-7081-', 'StartDate': '/Date(1457326119847)/', 'Year': 1999, 'WofExpires': '/Date(0)/', 'ExteriorColour': None, 'Vin': None, 'EngineSize': 0, 'Doors': 0, 'BodyStyle': 'Coupe', 'Title': 'Bentley Continental 1999', 'IsDealer': False, 'Make': 'Bentley', 'Transmission': 'Manual', 'Fuel': None, 'ImportHistory': None, 'Odometer': 2000, 'StereoDescription': None, 'Region': 'Manawatu', 'BestContactTime': None, 'Cylinders': 0, 'NumberPlate': None}]}
car_info = struct.get('List')
with open('car_info.csv', 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=car_info[0].keys())
writer.writeheader()
for row in car_info:
writer.writerow(row)