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
148
votes
16 answers

How to create an array from a CSV file using PHP and the fgetcsv function

Can someone kindly provide a code to create an array from a CSV file using fgetcsv? I've used the following code to create an array from a simple CSV file, but it doesn't work right when one of my fields has multiple commas - such as…
Thomas
  • 1,571
  • 3
  • 12
  • 12
146
votes
6 answers

In C#, how can I create a TextReader object from a string (without writing to disk)

I'm using A Fast CSV Reader to parse some pasted text into a webpage. The Fast CSV reader requires a TextReader object, and all I have is a string. What's the best way to convert a string into a TextReader object on the fly? Thanks! Update- Sample…
Hairgami_Master
  • 5,429
  • 10
  • 45
  • 66
146
votes
2 answers

Pandas df.to_csv("file.csv" encode="utf-8") still gives trash characters for minus sign

I've read something about a Python 2 limitation with respect to Pandas' to_csv( ... etc ...). Have I hit it? I'm on Python 2.7.3 This turns out trash characters for ≥ and - when they appear in strings. Aside from that the export is…
Maggie
  • 1,975
  • 3
  • 15
  • 17
143
votes
12 answers

Best timestamp format for CSV/Excel?

I'm writing a CSV file. I need to write timestamps that are accurate at least to the second, and preferably to the millisecond. What's the best format for timestamps in a CSV file such that they can be parsed accurately and unambiguously by Excel…
Jon
142
votes
9 answers

How to split csv whose columns may contain comma

Given 2,1016,7/31/2008 14:22,Geoff Dalgas,6/5/2011 22:21,http://stackoverflow.com,"Corvallis, OR",7679,351,81,b437f461b3fd27387c5d8ab47a293d35,34 How to use C# to split the above information into strings as follows: 2 1016 7/31/2008 14:22 Geoff…
q0987
  • 34,938
  • 69
  • 242
  • 387
141
votes
5 answers

How to read file with space separated values in pandas

I try to read the file into pandas. The file has values separated by space, but with different number of spaces I tried: pd.read_csv('file.csv', delimiter=' ') but it doesn't work
yemu
  • 26,249
  • 10
  • 32
  • 29
141
votes
18 answers

How to ignore the first line of data when processing CSV data?

I am asking Python to print the minimum number from a column of CSV data, but the top row is the column number, and I don't want Python to take the top row into account. How can I make sure Python ignores the first line? This is the code so…
user1496646
137
votes
6 answers

Skip rows during csv import pandas

I'm trying to import a .csv file using pandas.read_csv(), however, I don't want to import the 2nd row of the data file (the row with index = 1 for 0-indexing). I can't see how not to import it because the arguments used with the command seem…
thosphor
  • 2,493
  • 7
  • 26
  • 42
136
votes
19 answers

How can I turn a DataTable to a CSV?

Could somebody please tell me why the following code is not working. The data is saved into the csv file, however the data is not separated. It all exists within the first cell of each row. StringBuilder sb = new StringBuilder(); foreach…
Darren Young
  • 10,972
  • 36
  • 91
  • 150
136
votes
6 answers

PHP code to convert a MySQL query to CSV

What is the most efficient way to convert a MySQL query to CSV in PHP please? It would be best to avoid temp files as this reduces portability (dir paths and setting file-system permissions required). The CSV should also include one top line of…
multipolygon
  • 2,194
  • 2
  • 19
  • 23
134
votes
6 answers

How to write to a CSV line by line?

I have data which is being accessed via http request and is sent back by the server in a comma separated format, I have the following code : site= 'www.example.com' hdr = {'User-Agent': 'Mozilla/5.0'} req = urllib2.Request(site,headers=hdr) page =…
Mustard Tiger
  • 3,520
  • 8
  • 43
  • 68
134
votes
15 answers

Import data in MySQL from a CSV file using LOAD DATA INFILE

I am importing some data of 20,000 rows from a CSV file into MySQL. Columns in the CSV file are in a different order than MySQL tables' columns. How can I automatically assign columns corresponding to MySQL table columns? When I execute LOAD DATA…
MANJEET
  • 1,733
  • 2
  • 12
  • 21
132
votes
4 answers

Import CSV file as a Pandas DataFrame

How do I read the following CSV file into a Pandas…
mazlor
  • 1,795
  • 4
  • 19
  • 20
132
votes
10 answers

PHP Array to CSV

I'm trying to convert an array of products into a CSV file, but it doesn't seem to be going to plan. The CSV file is one long line, here is my code: for($i=0;$i
JohnnyFaldo
  • 4,121
  • 4
  • 19
  • 29
131
votes
13 answers

How to create CSV Excel file C#?

I'm looking for a class for creating CSV Excel files. Expected features: Extremely simple to use Escapes commas and quotes so excel handles them fine Exports date and datetimes in timezone-proof format Do you know any class capable of this?
Chris
  • 39,719
  • 45
  • 189
  • 235