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
15
votes
2 answers

How do I ensure D3 finishes loading several CSVs before javascript runs?

I'm loading a CSV of a list of other CSV files into javascript using D3. When I run the following code, the employees array is still empty by the time it gets to it in the code. Is there a correct way to ensure that D3 finishes loading the data…
mkwng
  • 151
  • 1
  • 3
15
votes
4 answers

Generate CSV file in ASP.Net

I am using the code below on an aspx page on button click event to generate csv file. This works when I do not name my file but when I try to use: Response.AddHeader("Content-Disposition", "attachment;filename=myfilename.csv"); to name the file…
user1178192
  • 163
  • 1
  • 1
  • 5
15
votes
4 answers

How to reverse column order of a file in Linux from the command line

I have a file named ip-list with two columns: IP1 Server1 IP2 Server2 And I want to produce: Server1 IP1 Server2 IP2 What's the most elegant, shortest Linux command line tool to do it?
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
15
votes
4 answers

When reading a CSV file using a DataReader and the OLEDB Jet data provider, how can I control column data types?

In my C# application I am using the Microsoft Jet OLEDB data provider to read a CSV file. The connection string looks like this: Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Data;Extended Properties="text;HDR=Yes;FMT=Delimited I open an ADO.NET…
Rory MacLeod
  • 11,012
  • 7
  • 41
  • 43
15
votes
4 answers

export csv in zend framework

I'm trying to export a database table as a .csv downloadable from the browser. My code is zend framework based and I'm almost there with the following action: public function exportTableAction() { $this->_helper->layout->disableLayout(); …
aimfeld
  • 2,931
  • 7
  • 32
  • 43
15
votes
3 answers

Is there such thing as a CSV Serializer? (similar to XmlSerializer)

I am toying around with serializing and deserializing CSV files and I am wondering if there is an existing library, similar in concept to the XmlSerializer, which can declaratively define objects and (de)serialize them to/from a file or stream. I…
Steve Konves
  • 2,648
  • 3
  • 25
  • 44
15
votes
8 answers

Preserving large numbers

I am trying to read a CSV file that has barcodes in the first column, but when R gets it into a data.frame, it converts 1665535004661 to 1.67E+12. Is there a way to preserve this number in an integer format? I tried assigning a class of "double",…
James
  • 1,447
  • 3
  • 16
  • 30
14
votes
2 answers

what "\",\x0A\x0D" code does in C# while writing CSV

Can anybody please tell me what its checking in following condition. if (s.IndexOfAny("\",\x0A\x0D".ToCharArray()) > -1)
user1171738
  • 143
  • 1
  • 1
  • 4
14
votes
2 answers

Python csv writer wrong separator?

Disclaimer: I'm in Europe. According to this page Excel uses the semicolon ; as default separator in Europe to "prevent conflicts" with the decimal comma. Now, I have this Python code: import csv data = [["test", "data"], ["foo", "bar"]] writer =…
orlp
  • 112,504
  • 36
  • 218
  • 315
14
votes
4 answers

Importing CSV data with Apache POI

How can I efficiently import CSV data with Apache POI? If I have a very large CSV file that I would like to store in my Excel spreadsheet, then I don't imagine that going cell-by-cell is the best way to import...?
Jake
  • 15,007
  • 22
  • 70
  • 86
14
votes
1 answer

Basics Introduction To Using CHCSVParser

I'm implementing CHCSVParser into my iPhone app (thanks Dave!) however I'm really confused on how to use it. I've read the read-me and searched some questions on SO but still not 100% sure what to do. I have a .CSV file with maybe 5000 rows of data…
Jon
  • 4,732
  • 6
  • 44
  • 67
14
votes
1 answer

pandas read_csv: The error_bad_lines argument has been deprecated and will be removed in a future version

I am trying to read some data which may sometimes have erroneous and bad rows, so as always I passed error_bad_lines=False but the console keeps throwing the deprecation warning on every run. Why is this feature deprecated and is there any other…
Atharva Katre
  • 457
  • 1
  • 6
  • 17
14
votes
3 answers

pandas read csv ignore ending semicolon of last column

My data file looks like…
user12587364
14
votes
5 answers

Python's CSV module vs. Pandas

I am using Pandas to read CSV file data, but the CSV module is also there to manage the CSV file. What is the difference between these both? What are the cons of using Pandas over the CSV module?
Aarsh
  • 370
  • 1
  • 3
  • 13
14
votes
3 answers

How can I define a Raku grammar to parse TSV text?

I have some TSV data ID Name Email 1 test test@email.com 321 stan stan@nowhere.net I would like to parse this into a list of hashes @entities[0] eq "test"; @entities[1] eq "stan@nowhere.net"; I'm having trouble…
littlebenlittle
  • 833
  • 2
  • 9
  • 18