Questions tagged [csv]

Comma-Separated Values or Character-Separated Values (CSV) is a standard "flat file database" format for storing tabular data in plain text, consisting of an optional header row that lists the table fields delimited by commas or tabs or other delimiter character, followed by one or more rows (newline separated) representing the table records as delimited lists of the values. Newlines and separator characters can appear within (quoted) fields.

CSV is a file format involving a plain text file with information separated by delimiters with the purpose of storing data in a table-structured format. CSV (comma separated values) files traditionally and most commonly use a comma delimiter (hence the name), but other characters can be used, such as semi-colons, tabs, pipe symbols (|), etc.

The MIME type for CSV files is text/csv.

Information is often stored in CSV format to make it easy to transfer tables of data between applications. Each row of a table is represented as a list of plain text (human-readable) values with a delimiter character between each discrete piece of data. Values may be enclosed in quotes, which is required if they contain the delimiter as a value. The first row of data often contains headers of table's columns, which describe the meaning of the data in each column.

Example

Tabular format

+-------+-------------+----------+----------------------+
| Time  | Temperature | Humidity | Description          |
+-------|-------------|----------|----------------------+
| 08:00 |     70      |    35    | Sunny and Clear      |
| 11:45 |     94      |    90    | Hazy, Hot, and Humid |
| 14:30 |     18      |          | Freezing             |
+-------+-------------+----------+----------------------+

CSV format

Time,Temperature,Humidity,Description
08:00,70,35,"Sunny and Clear"
11:45,94,90,"Hazy, Hot, and Humid"
14:30,18,,Freezing

In this example, the first row of CSV data serves as the "header" which describes the corresponding data below it. Each successive line of the CSV file would then neatly fit into the same field as the first line. There is no inherent way to describe within a CSV file whether the first row is a header row or not.

Note that empty fields (fields with no available data, such as the third field in the last line) are place-held with commas so that the fields that follow may be correctly placed.

Questions tagged are expected to relate to programming in some way, for example, parsing/importing CSV files or creating them programmatically.

Related links:

77596 questions
504
votes
5 answers

What MIME type should I use for CSV?

I've seen application/csv used and also text/csv. Is there a difference? Does it matter which as long as the request matches something that's available? Are they interchangeable?
Steve Dunn
  • 18,426
  • 11
  • 59
  • 85
486
votes
27 answers

Dealing with commas in a CSV file

I am looking for suggestions on how to handle a csv file that is being created, then uploaded by our customers, and that may have a comma in a value, like a company name. Some of the ideas we are looking at are: quoted Identifiers (value "," values…
Bob The Janitor
  • 19,164
  • 9
  • 45
  • 71
452
votes
39 answers

Python Pandas Error tokenizing data

I'm trying to use pandas to manipulate a .csv file but I get this error: pandas.parser.CParserError: Error tokenizing data. C error: Expected 2 fields in line 3, saw 12 I have tried to read the pandas docs, but found nothing. My code is…
abuteau
  • 4,823
  • 3
  • 13
  • 19
438
votes
13 answers

How to export table as CSV with headings on Postgresql?

I'm trying to export a PostgreSQL table with headings to a CSV file via command line, however I get it to export to CSV file, but without headings. My code looks as follows: COPY products_273 to '/tmp/products_199.csv' delimiters',';
Elitmiar
  • 30,202
  • 72
  • 172
  • 224
366
votes
14 answers

Is there a way to include commas in CSV columns without breaking the formatting?

I've got a two column CSV with a name and a number. Some people's name use commas, for example Joe Blow, CFA. This comma breaks the CSV format, since it's interpreted as a new column. I've read up and the most common prescription seems to be…
buley
  • 24,595
  • 17
  • 76
  • 99
362
votes
20 answers

Reading CSV file and storing values into an array

I am trying to read a *.csv-file. The *.csv-file consist of two columns separated by semicolon (";"). I am able to read the *.csv-file using StreamReader and able to separate each line by using the Split() function. I want to store each column into…
Rushabh Shah
  • 3,663
  • 3
  • 14
  • 4
356
votes
11 answers

Response Content type as CSV

I need to send a CSV file in HTTP response. How can I set the output response as CSV format? This is not working: Response.ContentType = "application/CSV";
balaweblog
  • 13,736
  • 28
  • 70
  • 93
338
votes
7 answers

How to add pandas data to an existing csv file?

I want to know if it is possible to use the pandas to_csv() function to add a dataframe to an existing csv file. The csv file has the same structure as the loaded data.
Ayoub Ennassiri
  • 3,515
  • 2
  • 11
  • 9
336
votes
6 answers

Create Pandas DataFrame from a string

In order to test some functionality I would like to create a DataFrame from a string. Let's say my test data looks like: TESTDATA="""col1;col2;col3 1;4.4;99 2;4.5;200 3;4.7;65 4;3.2;140 """ What is the simplest way to read that data into a Pandas…
Emil H
  • 16,495
  • 3
  • 39
  • 62
336
votes
19 answers

View tabular file such as CSV from command line

Anyone know of a command-line CSV viewer for Linux/OS X? I'm thinking of something like less but that spaces out the columns in a more readable way. (I'd be fine with opening it with OpenOffice Calc or Excel, but that's way too overpowered for…
Benjamin Oakes
  • 11,116
  • 11
  • 61
  • 80
326
votes
19 answers

How do I import CSV file into a MySQL table?

I have an unnormalized events-diary CSV from a client that I'm trying to load into a MySQL table so that I can refactor into a sane format. I created a table called 'CSVImport' that has one field for every column of the CSV file. The CSV contains 99…
Iain Samuel McLean Elder
  • 16,665
  • 10
  • 59
  • 76
302
votes
10 answers

Convert xlsx to csv in Linux with command line

I'm looking for a way to convert xlsx files to csv files on Linux. I do not want to use PHP/Perl or anything like that since I'm looking at processing several millions of lines, so I need something quick. I found a program on the Ubuntu repos called…
user1390150
  • 3,029
  • 2
  • 12
  • 3
298
votes
6 answers

Split a comma-delimited string into an array?

I need to split my string input into an array at the commas. Is there a way to explode a comma-separated string into a flat, indexed array? Input: 9,admin@example.com,8 Output: ['9', 'admin@example', '8']
Kevin
  • 21,638
  • 25
  • 75
  • 108
293
votes
17 answers

Parsing CSV files in C#, with header

Is there a default/official/recommended way to parse CSV files in C#? I don't want to roll my own parser. Also, I've seen instances of people using ODBC/OLE DB to read CSV via the Text driver, and a lot of people discourage this due to its…
David Pfeffer
  • 36,331
  • 28
  • 120
  • 198
283
votes
39 answers

How can I read and parse CSV files in C++?

I need to load and use CSV file data in C++. At this point it can really just be a comma-delimited parser (ie don't worry about escaping new lines and commas). The main need is a line-by-line parser that will return a vector for the next line each…
User1
  • 35,366
  • 60
  • 170
  • 251