dsci524_group29_webscraping.save_data ===================================== .. py:module:: dsci524_group29_webscraping.save_data Functions --------- .. autoapisummary:: dsci524_group29_webscraping.save_data.save_data Module Contents --------------- .. py:function:: save_data(data, format='csv', destination='output.csv') Saves the extracted data into a file. :param data: The data to be saved. - For 'csv', it must be a list of dictionaries where each dictionary represents a row. - For 'json', it can be either a list or a dictionary. :type data: list or dict :param format: The format in which to save the data. Options are: - 'csv': Saves the data as a CSV file. Each key in the dictionaries becomes a column header. - 'json': Saves the data as a JSON file. The data is serialized with indentation for readability. Default is 'csv'. :type format: str, optional :param destination: The file path to save the data. Can specify: - A file name (e.g., 'output.csv'). - A full path (e.g., '/path/to/output.csv'). Default is 'output.csv'. :type destination: str, optional :returns: The absolute path to the saved file. :rtype: str :raises ValueError: If the format is unsupported or if the data structure is incompatible with the format. :raises FileNotFoundError: If the directory specified in the destination path does not exist. :raises Exception: If an unexpected error occurs during the file-writing process. .. rubric:: Examples # Save data as a CSV file save_data([{"name": "Alice", "age": 25}, {"name": "Bob", "age": 30}], format='csv', destination='data.csv') # Save data as a JSON file save_data({"name": "Alice", "age": 25}, format='json', destination='data.json') .. rubric:: Notes - The directory specified in the destination path must exist; otherwise, a FileNotFoundError is raised. - For 'csv', the first dictionary in the list determines the column headers.