Comma-separated values
Comma-separated values | |
---|---|
![]() | |
Filename extension | .csv |
Internet media type | text/csv [1] |
Uniform Type Identifier (UTI) | public.comma-separated-values-text[2] |
UTI conformation | public.delimited-values-text[2] |
Type of format | multi-platform, serial data streams |
Container for | database information organized as field separated lists |
Standard | RFC 4180 |
Comma-separated values (CSV) is a text data format that uses commas to separate delimiter-separated values, and newlines to separate records. CSV data stores tabular data (numbers and text) in plain text, where each line typically represents one data record. Each record consists of the same number of fields, and these are separated by commas. If the field delimiter itself may appear within a field, fields can be surrounded with quotation marks.[3]
CSV is widespread in data applications and is widely supported by a variety of software, including common spreadsheet applications such as Microsoft Excel.[4] Benefits cited in favor of CSV include human readability and the simplicity of the format.[5]
The CSV file format was formalized in the 2005 technical standard RFC 4180, which defines the MIME type "text/csv" for the handling of text-based fields.
History
[edit]Comma-separated values is a data format that predates personal computers by more than a decade: the IBM Fortran (level H extended) compiler under OS/360 supported CSV in 1972.[6] List-directed ("free form") input/output was defined in FORTRAN 77, approved in 1978. List-directed input used commas or spaces for delimiters, so unquoted character strings could not contain commas or spaces.[7]
The term "comma-separated value" and the "CSV" abbreviation were in use by 1983.[8] The manual for the Osborne Executive computer, which bundled the SuperCalc spreadsheet, documents the CSV quoting convention that allows strings to contain embedded commas.[9]
Comma-separated value lists are easier to type (for example into punched cards) than fixed-column-aligned data, and they were less prone to producing incorrect results if a value was punched one column off from its intended location.
Comma separated files are used for the interchange of database information between machines of two different architectures. The plain-text character of CSV files largely avoids incompatibilities such as byte-order and word size. The files are largely human-readable, so it is easier to deal with them in the absence of perfect documentation or communication.[10]
The main standardization initiative—transforming "de facto fuzzy definition" into a more precise and de jure one—was in 2005, with RFC 4180, defining CSV as a MIME Content Type.[11] Later, in 2013, some of RFC 4180's deficiencies were tackled by a W3C recommendation.[12]
In 2014 IETF published RFC 7111 describing the application of URI fragments to CSV documents. RFC 7111 specifies how row, column, and cell ranges can be selected from a CSV document using position indexes.[13]
In 2015 W3C, in an attempt to enhance CSV with formal semantics, publicized the first drafts of recommendations for CSV metadata standards, which began as recommendations in December of the same year.[14]
Specification
[edit]Casually, the term "CSV" might refer to any file that:[1][15]
- Is plain text using a character encoding such as ASCII, various Unicode character encodings (e.g. UTF-8), EBCDIC, or Shift JIS;
- Consists of records (typically one record per line);
- Divided its records into fields separated by a comma;
- Has the same sequence of fields for each record.
The 2005 technical standard RFC 4180 formalizes the CSV file format and defines the MIME type "text/csv" for the handling of text-based fields. However, the interpretation of the text of each field is still application-specific. Files that follow the RFC 4180 standard can simplify CSV exchange and should be widely portable. Among its requirements:
- MS-DOS-style lines that end with (CR/LF) characters (optional for the last line).
- An optional header record (there is no sure way to detect whether it is present, so care is required when importing).
- Each record should contain the same number of comma-separated fields.
- Any field may be quoted (with double quotes).
- Fields containing a line-break, double-quote or commas should be quoted. (If they are not, the file will likely be impossible to process correctly.)
- If double-quotes are used to enclose fields, then a double-quote in a field must be represented by two double-quote characters.
The format can be processed by most programs that claim to read CSV files. The exceptions are (a) programs may not support line-breaks within quoted fields, (b) programs may confuse the optional header with data or interpret the first data line as an optional header, and (c) double-quotes in a field may not be parsed correctly automatically.
In 2011 Open Knowledge Foundation (OKF) and various partners created a data protocols working group, which later evolved into the Frictionless Data initiative. One of the main formats they released was the Tabular Data Package. Tabular Data package was heavily based on CSV, using it as the main data transport format and adding basic type and schema metadata (CSV lacks any type information to distinguish the string "1" from the number 1).[16] The Frictionless Data Initiative has also provided a standard CSV Dialect Description Format for describing different dialects of CSV, for example specifying the field separator or quoting rules.[17]
In 2013 the W3C "CSV on the Web" working group began to specify technologies providing higher interoperability for web applications using CSV or similar formats.[18] The working group completed its work in February 2016 and is officially closed in March 2016 with the release of a set of documents and W3C recommendations[19] for modeling "Tabular Data",[14] and enhancing CSV with metadata and semantics. While the well-formedness of CSV data can readily checked, testing validity and canonical form is less well developed, relative to more precise data formats, such as XML and SQL, which offer richer types and rules-based validation.[20]
Features
[edit]CSV formats are best used to represent sets or sequences of records in which each record has an identical list of fields. This corresponds to a single relation in a relational database, or to data (though not calculations) in a typical spreadsheet.
The format dates back to the early days of business computing and is widely used to pass data between computers with different internal word sizes, data formatting needs, and so forth. For this reason, CSV files are common on all computer platforms.
CSV is a delimited text file that uses a comma to separate values (many implementations of CSV import/export tools allow other separators to be used; for example, the use of a "Sep=^" row as the first row in the *.csv file will cause Excel to open the file expecting caret "^" to be the separator instead of comma ","). Simple CSV implementations may prohibit field values that contain a comma or other special characters such as newlines. More sophisticated CSV implementations permit them, often by requiring " (double quote) characters around values that contain reserved characters (such as commas, double quotes, or less commonly, newlines). Embedded double quote characters may then be represented by a pair of consecutive double quotes,[21] or by prefixing a double quote with an escape character such as a backslash (for example in Sybase Central).
CSV formats are not limited to a particular character set.[1] They work just as well with Unicode character sets (such as UTF-8 or UTF-16) as with ASCII (although particular programs that support CSV may have their own limitations). CSV files normally will even survive naïve translation from one character set to another (unlike nearly all proprietary data formats). CSV does not, however, provide any way to indicate what character set is in use, so that must be communicated separately, or determined at the receiving end (if possible).
Applications
[edit]CSV is a common data exchange format that is widely supported by consumer, business, and scientific applications. Among its most common uses is moving tabular data[22][23] between programs that natively operate on incompatible (often proprietary or undocumented) formats.[1] For example, a user may need to transfer information from a database program that stores data in a proprietary format, to a spreadsheet that uses a completely different format. Most database programs can export data as CSV. Most spreadsheet programs can read CSV data, allowing CSV to be used as an intermediate format when transferring data from a database to a spreadsheet. Every major ecommerce platform provides support for exporting data as a CSV file.[24]
CSV is also used for storing data. Common data science tools such as Pandas include the option to export data to CSV for long-term storage.[25] Benefits of CSV for data storage include the simplicity of CSV makes parsing and creating CSV files easy to implement and fast compared to other data formats, human readability making editing or fixing data simpler,[26] and high compressibility leading to smaller data files.[27] Alternatively, CSV does not support more complex data relations and makes no distinction between null and empty values, and in applications where these features are needed other formats are preferred.
More than 200 local, regional, and national data portals, such as those of the UK government and the European Commission, use CSV files with standardized data catalogs.[28]
Some applications use CSV as a data interchange format to enhance its interoperability, exporting and importing CSV. Others use CSV as an internal format.
- As a data interchange format
- CSV is supported by almost all spreadsheets and database management systems.
- Spreadsheets including Apple Numbers, LibreOffice Calc, and Apache OpenOffice Calc. Microsoft Excel also supports a dialect of CSV with restrictions in comparison to other spreadsheet software (e.g., as of 2019[update] Excel still cannot export CSV files in the commonly used UTF-8 character encoding, and separator is not enforced to be the comma). LibreOffice Calc CSV importer is actually a more generic delimited text importer, supporting multiple separators at the same time as well as field trimming.
- Various Relational databases support saving query results to a CSV file. PostgreSQL provides the
COPY
command, which allows for both saving and loading data to and from a file.COPY (SELECT * FROM articles) TO '/home/wikipedia/file.csv' (FORMAT csv)
saves the content of a tablearticles
to a file called/home/wikipedia/file.csv
.[29]
- As (main or optional) internal representation
- Can be native or foreign, but differ from interchange format ("export/import only") because it is not necessary to create a copy in another format:
- Some Spreadsheets including LibreOffice Calc offers this option, without enforcing user to adopt another format.
- Some relational databases, when using standard SQL, offer foreign-data wrapper (FDW). For example, PostgreSQL offers the
CREATE FOREIGN TABLE
[30] andCREATE EXTENSION file_fdw
[31] commands to configure any variant of CSV. - Databases like Apache Hive offer the option to express CSV or .csv.gz as an internal table format.
- The emacs editor can operate on CSV files using csv-nav mode.[32]
CSV format is supported by libraries available for many programming languages. Most provide some way to specify the field delimiter, decimal separator, character encoding, quoting conventions, date format, etc.
Programs that work with CSV may have limits on the maximum number of rows CSV files can have. Examples include Microsoft Excel (1,048,576 rows), Apple Numbers (1,000,000 rows), Google Sheets (10,000,000 cells), and OpenOffice and LibreOffice (1,048,576 rows).[33]
See also
[edit]- Comparison of data-serialization formats
- Delimiter collision – Character(s) for specifying the boundary between regions of data
- Flat-file database – Database stored as flat data
- Simple Data Format
References
[edit]- ^ a b c d Shafranovich, Y. (October 2005). Common Format and MIME Type for CSV Files. IETF. p. 1. doi:10.17487/RFC4180. RFC 4180.
- ^ a b "commaSeparatedText". Apple Developer Documentation: Uniform Type Identifiers. Apple Inc. Archived from the original on 2023-05-22. Retrieved 2023-05-22.
- ^ "CSV Comma Separated Value File Format - How To - Creativyst - Explored,Designed,Delivered.(sm)". Creativyst Software. Archived from the original on 1 April 2021. Retrieved 22 August 2023.
- ^ "Import or export text (.txt or .csv) files". Microsoft Support. Retrieved 2023-08-16.
- ^ "What is a CSV file: A comprehensive guide". flatfile.com. Retrieved 2024-10-28.
- ^ IBM FORTRAN Program Products for OS and the CMS Component of VM/370 General Information (PDF) (first ed.), July 1972, p. 17, GC28-6884-0, archived (PDF) from the original on March 4, 2016, retrieved February 5, 2016,
For users familiar with the predecessor FORTRAN IV G and H processors, these are the major new language capabilities
- ^ "List-Directed I/O", Fortran 77 Language Reference, Oracle, archived from the original on 2021-02-26, retrieved 2012-10-26
- ^ "SuperCalc², spreadsheet package for IBM, CP/M". Retrieved December 11, 2017.
- ^ "Comma-Separated-Value Format File Structure". 1983. Retrieved December 11, 2017.
- ^ "CSV, Comma Separated Values (RFC 4180)". Retrieved June 4, 2014.
- ^ Common Format and MIME Type for Comma-Separated Values (CSV) Files. doi:10.17487/RFC4180. RFC 4180. Retrieved December 22, 2020.
- ^ See sparql11-results-csv-tsv, the first W3C recommendation scoped in CSV and filling some of RFC 4180's deficiencies.
- ^ URI Fragment Identifiers for the text/csv Media Type. doi:10.17487/RFC7111. RFC 7111. Retrieved December 22, 2020.
- ^ a b "Model for Tabular Data and Metadata on the Web". 17 December 2015. Retrieved March 23, 2016. (W3C Recommendation)
- ^ "Comma Separated Values (CSV) Standard File Format". Edoceo, Inc. Archived from the original on July 14, 2020. Retrieved June 4, 2014.
- ^ "Tabular Data Package". Frictionless Data Specs.
- ^ "CSV Dialect". Frictionless Data Specs.
- ^ "CSV on the Web Working Group". W3C CSV WG. 2013. Retrieved 2015-04-22.
- ^ "CSV on the Web Repository". (on GitHub)
- ^ "Rules Or Schemas". CsvPath Project. 2024. Retrieved 2025-02-13.
- ^ *Creativyst (2010), How To: The Comma Separated Value (CSV) File Format, creativyst.com, archived from the original on April 4, 2021, retrieved May 24, 2010
- ^ "CSV - Comma Separated Values". Archived from the original on 2021-03-07. Retrieved 2017-12-02.
- ^ "CSV Files". Archived from the original on April 30, 2021. Retrieved June 4, 2014.
- ^ "CSV Supported Ecommerce Platforms". RFM Calc. Retrieved 2025-03-09.
- ^ "pandas.DataFrame.to_csv — pandas 2.0.3 documentation". pandas.pydata.org. Retrieved 2023-08-16.
- ^ "CSV Format: History, Advantages and Why It Is Still Popular". ByteScout. 2021-09-15. Retrieved 2023-08-16.
- ^ "Comparison of different file formats in Big Data". www.adaltas.com. 2020-07-23. Retrieved 2023-08-16.
- ^ Mahmud, S M Hasan; Hossin, Md Altab; Jahan, Hosney; Noori, Sheak Rashed Haider; Bhuiyan, Touhid (2018). "CSV-ANNOTATE: Generate annotated tables from CSV file". 2018 International Conference on Artificial Intelligence and Big Data (ICAIBD). IEEE. pp. 71–75. doi:10.1109/ICAIBD.2018.8396169. ISBN 978-1-5386-6987-7.
- ^ "Documentation: 14: COPY". PostgreSQL. Retrieved 2024-05-12.
- ^ "Documentation: 14: F.35. postgres_fdw". PostgreSQL. 2022-02-10. Retrieved 2022-03-04.
- ^ "Documentation: 14: F.14. file_fdw". PostgreSQL. 2022-02-10. Retrieved 2022-03-04.
- ^ "EmacsWiki: Csv Nav". www.emacswiki.org.
- ^ "Understanding CSV and row limits". Archived from the original on January 15, 2021. Retrieved Feb 28, 2021.
Further reading
[edit]- "IBM DB2 Administration Guide - LOAD, IMPORT, and EXPORT File Formats". IBM. Archived from the original on 2016-12-13. Retrieved 2016-12-12. (Has file descriptions of delimited ASCII (.DEL) (including comma- and semicolon-separated) and non-delimited ASCII (.ASC) files for data transfer.)