--- title: "system.dataset.toCSV" description: "Formats the contents of a dataset as CSV (comma separated values), returning the resulting CSV as a string." --- # system.dataset.toCSV > Formats the contents of a dataset as CSV (comma separated values), returning the resulting CSV as a string. This function is used in **Python Scripting**. ## Description Formats the contents of a [dataset](platform/scripting/python-scripting/variables-datatypes-and-objects/datasets.md) as CSV (comma separated values), returning the resulting CSV as a string. If the "forExport" flag is set, then the format will be appropriate for parsing using the [system.dataset.fromCSV](appendix\scripting-functions\system-dataset\system-dataset-fromCSV.md) function. ## Client Permission Restrictions This scripting function has no [Client Permission](../../../ignition-modules/vision/vision-project-properties/vision-project-properties.md#vision-permissions-properties) restrictions. ## Syntax `system.dataset.toCSV(dataset, showHeaders, forExport, localized)` ### Parameters Type | Parameter | Description ------- | ------- | ------- Dataset | `dataset` | The dataset to export to CSV. Boolean | `showHeaders` | If set to true, a header row will be present in the CSV. Default is true. Boolean | `forExport` | If set to true, extra header information will be present in the CSV data which is necessary for the CSV to be compatible with the fromCSV method. Overrides showHeaders. Default is false. Boolean | `localized` | If set to true, the string representations of the values in the CSV data will be localized. Default is false. ### Returns **String** - The CSV data as a string. ### Scope Gateway, Vision Client, Perspective Session ## Code Examples ```python title="Code Snippet" # This snippet runs a SQL query against a database and turns the results into a CSV string. It would # then store the resulting CSV to a file on the local hard drive. results = system.db.runNamedQuery("Fetch Records",{}) csv = system.dataset.toCSV(dataset = results, showHeaders = True, forExport = False) filePath = "C:\\output\\results.csv" system.file.writeFile(filePath, csv) ```