append_dict_to_csv

ssapy.io.append_dict_to_csv(file_name, data_dict, delimiter='\t')[source][source]

Append data from a dictionary to a CSV file.

This function appends rows of data to a CSV file, where each key-value pair in the dictionary represents a column. If the CSV file does not already exist, it creates the file and writes the header row using the dictionary keys.

Parameters:

file_namestr

Path to the CSV file where data will be appended.

data_dictdict

Dictionary where keys are column headers and values are lists of data to be written to the CSV file. All lists should be of the same length.

delimiterstr, optional

The delimiter used in the CSV file (default is tab ` `).

Notes:

  • The function assumes that all lists in the dictionary data_dict have the same length.

  • If the CSV file already exists, only the data rows are appended. If it doesn’t exist, a new file is created with the header row based on the dictionary keys.

  • The delimiter parameter allows specifying the delimiter used in the CSV file. Common values are , for commas and ` ` for tabs.

Example:

>>> data_dict = {
>>>     'Name': ['Alice', 'Bob', 'Charlie'],
>>>     'Age': [25, 30, 35],
>>>     'City': ['New York', 'Los Angeles', 'Chicago']
>>> }
>>> append_dict_to_csv('people.csv', data_dict, delimiter=',')
This will append data to 'people.csv', creating it if it does not exist, with columns 'Name', 'Age', 'City'.

Dependencies:

  • os.path.exists: Used to check if the file already exists.

  • csv: Standard library module used for reading and writing CSV files.