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
194
votes
12 answers

How to write to an Excel spreadsheet using Python?

I need to write some data from my program to an Excel spreadsheet. I've searched online and there seem to be many packages available (xlwt, XlsXcessive, openpyxl). Others suggest writing to a .csv file (never used CSV and don't really understand…
Jey
  • 2,199
  • 2
  • 14
  • 15
191
votes
9 answers

MySQL load NULL values from CSV data

I have a file that can contain from 3 to 4 columns of numerical values which are separated by comma. Empty fields are defined with the exception when they are at the end of the row: 1,2,3,4,5 1,2,3,,5 1,2,3 The following table was created in…
Spiros
  • 2,231
  • 3
  • 15
  • 26
186
votes
38 answers

How to split a comma-separated value to columns

I have a table like this Value String 1 Cleo, Smith I want to separate the comma delimited string into two columns Value Name Surname 1 Cleo Smith I need only two fixed extra columns
Gurru
  • 2,013
  • 3
  • 17
  • 20
185
votes
6 answers

How to parse a CSV file using PHP

Suppose I have a .csv file with the following content: "text, with commas","another text",123,"text",5; "some without commas","another text",123,"text"; "some text with commas or no",,123,"text"; How can I parse the content through PHP?
smith
  • 5,341
  • 8
  • 31
  • 38
185
votes
10 answers

Export to CSV via PHP

Let's say I have a database.... is there a way I can export what I have from the database to a CSV file (and text file [if possible]) via PHP?
test
  • 17,706
  • 64
  • 171
  • 244
178
votes
19 answers

Parsing a CSV file using NodeJS

With nodejs I want to parse a .csv file of 10000 records and do some operation on each row. I tried using http://www.adaltas.com/projects/node-csv. I couldnt get this to pause at each row. This just reads through all the 10000 records. I need to do…
lonelymo
  • 3,972
  • 6
  • 28
  • 36
175
votes
19 answers

Generating CSV file for Excel, how to have a newline inside a value

I need to generate a file for Excel, some of the values in this file contain multiple lines. there's also non-English text in there, so the file has to be Unicode. The file I'm generating now looks like this: (in UTF8, with non English text mixed in…
Nir
  • 29,306
  • 10
  • 67
  • 103
172
votes
10 answers

CSV API for Java

Can anyone recommend a simple API that will allow me to use read a CSV input file, do some simple transformations, and then write it. A quick google has found http://flatpack.sourceforge.net/ which looks promising. I just wanted to check what others…
David Turner
  • 5,014
  • 6
  • 26
  • 27
170
votes
6 answers

How do I write data into CSV format as string (not file)?

I want to cast data like [1,2,'a','He said "what do you mean?"'] to a CSV-formatted string. Normally one would use csv.writer() for this, because it handles all the crazy edge cases (comma escaping, quote mark escaping, CSV dialects, etc.) The…
Li-aung Yip
  • 12,320
  • 5
  • 34
  • 49
170
votes
16 answers

Write single CSV file using spark-csv

I am using https://github.com/databricks/spark-csv , I am trying to write a single CSV, but not able to, it is making a folder. Need a Scala function which will take parameter like path and file name and write that CSV file.
user1735076
  • 3,225
  • 7
  • 19
  • 16
169
votes
9 answers

Rearrange columns using cut

I am having a file in the following format Column1 Column2 str1 1 str2 2 str3 3 I want the columns to be rearranged. I tried below command cut -f2,1 file.txt The command doesn't reorder the columns. Any idea why its not…
Boolean
  • 14,266
  • 30
  • 88
  • 129
169
votes
17 answers

How to extract one column of a csv file

If I have a csv file, is there a quick bash way to print out the contents of only any single column? It is safe to assume that each row has the same number of columns, but each column's content would have different length.
user788171
  • 16,753
  • 40
  • 98
  • 125
168
votes
6 answers

Get pandas.read_csv to read empty values as empty string instead of nan

I'm using the pandas library to read in some CSV data. In my data, certain columns contain strings. The string "nan" is a possible value, as is an empty string. I managed to get pandas to read "nan" as a string, but I can't figure out how to get…
BrenBarn
  • 242,874
  • 37
  • 412
  • 384
167
votes
12 answers

How to export data as CSV format from SQL Server using sqlcmd?

I can quite easily dump data into a text file such as: sqlcmd -S myServer -d myDB -E -Q "select col1, col2, col3 from SomeTable" -o "MyData.txt" However, I have looked at the help files for SQLCMD but have not seen an option specifically for…
Ray
  • 187,153
  • 97
  • 222
  • 204
165
votes
16 answers

How do I spool to a CSV formatted file using SQLPLUS?

I want to extract some queries to a CSV output format. Unfortunately, I can't use any fancy SQL client or any language to do it. I must use SQLPLUS. How do I do it?
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681