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
13
votes
4 answers

How to format heading in excel / csv using C#

My code will generate the excel document like this |id | Name | Address | company_Name | Destination| |----|-------|----------|--------------|------------| |##1 | xxx | xxxx | xxx | xxxxx | But I want like…
GOPI
  • 1,042
  • 8
  • 30
13
votes
2 answers

Write different data frame in one .csv file with R

I have 3 data frame, and I want them to be written in one single .csv file, one above the others, not in a same table. So, 3 different tables in one csv file. They all have same size. The problem with write.csv: it does not contain "append"…
David
  • 4,785
  • 7
  • 39
  • 63
13
votes
6 answers

How to Transform a String from Multi-Line to Single-Line in PHP?

Is there a PHP string function that transforms a multi-line string into a single-line string? I'm getting some data back from an API that contains multiple lines. For example:

Some Data

Some more Data

Even More Data

I assign…
edt
  • 22,010
  • 30
  • 83
  • 118
13
votes
4 answers

How do I remove lines of data in the middle of a text file with Ruby

I know how to write to a file, and read from a file, but I don't know how to modify a file besides reading the entire file into memory, manipulating it, and rewriting the entire file. For large files this isn't very productive. I don't really know…
Senjai
  • 1,811
  • 3
  • 20
  • 40
13
votes
11 answers

How do I parse out the fields in a comma separated string using sscanf while supporting empty fields?

I have a comma separated string which might contain empty fields. For example: 1,2,,4 Using a basic sscanf(string,"%[^,],%[^,],%[^,],%[^,],%[^,]", &val1, &val2, &val3, &val4); I get all the values prior to the empty field, and unexpected results…
Belrog
  • 950
  • 1
  • 11
  • 19
13
votes
2 answers

Using CSV file to read test data from

I need to test a various links of a site (no need to login) with 100's of users and loop it for some number of times using JMeter. I want to put those links in a "CSV file", so that all the links to be tested are read from file. How do I accomplish…
user2003333
  • 149
  • 1
  • 1
  • 3
13
votes
3 answers

How to use Google Chart with data from a csv

I have a csv file that looks like that: week,value1,value2 1,2,3 2,7,9 I would like to plot a stacked graph of it using google chart (week being my x (horizontal) values and values1 and values2 being the two set of y's). Unfortunately, I didn't…
cpa
  • 3,742
  • 4
  • 16
  • 22
13
votes
3 answers

Pandas read_csv and UTF-16

I have a CSV text file encoded in UTF-16 (so as to preserve Unicode characters when others use Excel) but when doing a read_csv with Pandas 0.9.0, I get this cryptic error: df =…
Brian Keegan
  • 2,208
  • 4
  • 24
  • 31
13
votes
4 answers

How can I strip all line breaks to generate a proper CSV?

I have a textarea submitting to my database on a website that is properly working. But when I generate a CSV (via PHP) from my database, all line breaks will mess up with the resulting CSV. Any CSV reader will interpret the line break from the input…
Gus Fune
  • 643
  • 2
  • 5
  • 21
13
votes
5 answers

Prompt a csv file to download as pop up using node.js and node-csv-parser (node module)

Recently I have started working with node.js. While going through a requirement in one of my projects I am facing an issue where I should be able to write some data to a csv file dynamically and let it prompt as a popup to download for user (with…
Manish Kumar
  • 15,269
  • 5
  • 18
  • 27
13
votes
5 answers

How do I detect missing fields in a CSV file in a Pythonic way?

I'm trying to parse a CSV file using Python's csv module (specifically, the DictReader class). Is there a Pythonic way to detect empty or missing fields and throw an error? Here's a sample file using the following headers: NAME, LABEL,…
bedwyr
  • 5,774
  • 4
  • 31
  • 49
13
votes
4 answers

Batch Combine CSV Remove Header

I have multiple CSV files with the same header and I'm trying to combine them together in Batch and keep only a single header. Any ideas?
Ken J
  • 4,312
  • 12
  • 50
  • 86
13
votes
2 answers

Escape Comma from CSV in Ruby

I'm creating a CSV download in my Ruby application which has some Dollar amount fields. These fields has data in cyrrency format like $35,456 and when such data comes in CSV it get seperated into two fiedls , data after comma moved to next column in…
Anil D
  • 1,989
  • 6
  • 29
  • 60
13
votes
2 answers

Bash: sort csv file by first 4 columns

I have a csv file with fields delimited by ";". There are 8 fields, and I want to sort my data by the first 4 columns, in increasing order (first sort by column 1, then column 2, etc) How I can do this from a command line in linux? I tried with open…
Ricky Robinson
  • 21,798
  • 42
  • 129
  • 185
13
votes
3 answers

Trying to split string into single words or "quoted words", and want to keep the quotes in the resulting array

I'm trying to split a string like Presentation about "Test Driven Development" into an array like this: [ 'Presentation', 'about', '"Behavior Driven Development"' ] I have tried CSV::parse_line(string, col_sep: ' '), but this results in […
Joshua Muheim
  • 12,617
  • 9
  • 76
  • 152