Questions tagged [csv]

Comma-Separated Values or Character-Separated Values (CSV) is a common "flat file database" (or spreadsheet-style) format for storing tabular data in plain text, with fields separated by a special character (comma, tab, etc). Rows are typically denoted by newline characters. Use for any delimited file formats, including tab delimited (TSV)

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
16:00 -200 "Unliveable"

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
16:00,-200,,""Unliveable""

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

Note:

  • 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.
  • Since the comma is the delimiter for fields, the commas in the Description field of the second line must be quoted (to prevent them from being interpreted as field delimiters). Wrapping the entire field in double quotes (") is the default method for protecting the delimiter character inside a field.
  • Since the double-quote is the delimiter quote character, double-quotes in the data, as in "Unliveable" on the fourth line, must also be protected. Doubling-up the double-quote is the default method for protecting the quote character inside a field.

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

Related links:

89606 questions
225
votes
17 answers

Creating a dictionary from a csv file?

I am trying to create a dictionary from a csv file. The first column of the csv file contains unique keys and the second column contains values. Each row of the csv file represents a unique key, value pair within the dictionary. I tried to use the…
drbunsen
  • 10,139
  • 21
  • 66
  • 94
225
votes
20 answers

Create a CSV File for a user in PHP

I have data in a MySQL database. I am sending the user a URL to get their data out as a CSV file. I have the e-mailing of the link, MySQL query, etc. covered. How can I, when they click the link, have a pop-up to download a CVS with the record from…
Jason
222
votes
26 answers

How can I output a UTF-8 CSV in PHP that Excel will read properly?

I've got this very simple thing that just outputs some stuff in CSV format, but it's got to be UTF-8. I open this file in TextEdit or TextMate or Dreamweaver and it displays UTF-8 characters properly, but if I open it in Excel it's doing this silly…
Ben Saufley
  • 3,259
  • 5
  • 27
  • 42
221
votes
6 answers

Output array to CSV in Ruby

It's easy enough to read a CSV file into an array with Ruby but I can't find any good documentation on how to write an array into a CSV file. Can anyone tell me how to do this? I'm using Ruby 1.9.2 if that matters.
Jason Swett
  • 43,526
  • 67
  • 220
  • 351
216
votes
15 answers

Read specific columns from a csv file with csv module?

I'm trying to parse through a csv file and extract the data from only specific columns. Example csv: ID | Name | Address | City | State | Zip | Phone | OPEID | IPEDS | 10 | C... | 130 W.. | Mo.. | AL... | 3.. | 334.. | 01023 | 10063 | I'm trying to…
frankV
  • 5,353
  • 8
  • 33
  • 46
214
votes
12 answers

Reading CSV files using C#

I'm writing a simple import application and need to read a CSV file, show result in a DataGrid and show corrupted lines of the CSV file in another grid. For example, show the lines that are shorter than 5 values in another grid. I'm trying to do…
ilkin
  • 2,862
  • 3
  • 20
  • 20
213
votes
9 answers

How to convert arbitrary simple JSON to CSV using jq?

Using jq, how can arbitrary JSON encoding an array of shallow objects be converted to CSV? There are plenty of Q&As on this site that cover specific data models which hard-code the fields, but answers to this question should work given any JSON,…
outis
  • 75,655
  • 22
  • 151
  • 221
207
votes
23 answers

How to use mongoimport to import CSV files?

CSV file with contact information: Name,Address,City,State,ZIP Jane Doe,123 Main St,Whereverville,CA,90210 John Doe,555 Broadway Ave,New York,NY,10010 Running this doesn't add documents to the database: $ mongoimport -d mydb -c things --type…
Joe
  • 25,307
  • 12
  • 38
  • 35
206
votes
15 answers

Save Dataframe to csv directly to s3 Python

I have a pandas DataFrame that I want to upload to a new CSV file. The problem is that I don't want to save the file locally before transferring it to s3. Is there any method like to_csv for writing the dataframe to s3 directly? I am using…
user2494275
  • 2,063
  • 2
  • 13
  • 4
204
votes
22 answers

Microsoft Excel mangles Diacritics in .csv files?

I am programmatically exporting data (using PHP 5.2) into a .csv test file. Example data: Numéro 1 (note the accented e). The data is utf-8 (no prepended BOM). When I open this file in MS Excel is displays as Numéro 1. I am able to open this in a…
Freddo411
  • 2,293
  • 3
  • 18
  • 17
204
votes
23 answers

How to read a CSV file into a .NET Datatable

How can I load a CSV file into a System.Data.DataTable, creating the datatable based on the CSV file? Does the regular ADO.net functionality allow this?
Ronnie Overby
  • 45,287
  • 73
  • 267
  • 346
201
votes
6 answers

csv.Error: iterator should return strings, not bytes

Sample.csv contains the following: NAME Id No Dept Tom 1 12 CS Hendry 2 35 EC Bahamas 3 21 IT Frank 4 61 EE And the Python file contains the following code: import csv ifile = open('sample.csv', "rb") read =…
user1094976
198
votes
7 answers

How to correctly display .csv files within Excel 2013?

It seems Excel 2013 doesn't read CSV files correctly (Excel 2010 does). Every time I open .csv files, all my data are displayed in the first column. I know I can go to DATA, Convert, and then choose commas, but this set up is not saved, and I am fed…
David
  • 4,785
  • 7
  • 39
  • 63
197
votes
3 answers

Python Pandas: How to read only first n rows of CSV files in?

I have a very large data set and I can't afford to read the entire data set in. So, I'm thinking of reading only one chunk of it to train but I have no idea how to do it.
bensw
  • 2,818
  • 5
  • 21
  • 31
195
votes
2 answers

Prevent row names to be written to file when using write.csv

Commands: t <- data.frame(v = 5:1, v2 = 9:5) write.csv(t, "t.csv") Resulting file: # "","v","v2" # "1",5,9 # "2",4,8 # "3",3,7 # "4",2,6 # "5",1,5 How do I prevent first column with row index from being written to the file?
watbywbarif
  • 6,487
  • 8
  • 50
  • 64