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

Combine columns from several CSV files into a single file

I have a bunch of CSV files (only two in the example below). Each CSV file has 6 columns. I want to go into each CSV file, copy the first two columns and add them as new columns to an existing CSV file. Thus far I have: import csv f =…
Stylize
  • 1,058
  • 5
  • 16
  • 32
14
votes
11 answers

using bash (sed/awk) to extract rows AND columns in CSV files?

Is bash capable of handling extracting rows and columns from csv files? Hoping I don't have to resort to python.. My 5-column csv file looks like: Rank,Name,School,Major,Year 1,John,Harvard,Computer Science,3 2,Bill,Yale,Political…
user1899415
  • 3,015
  • 7
  • 22
  • 31
14
votes
1 answer

beautifulSoup html csv

Good evening, I have used BeautifulSoup to extract some data from a website as follows: from BeautifulSoup import BeautifulSoup from urllib2 import urlopen soup = BeautifulSoup(urlopen('http://www.fsa.gov.uk/about/media/facts/fines/2002')) table =…
merlin_1980
  • 207
  • 2
  • 3
  • 9
14
votes
2 answers

php fgetcsv - charset encoding problems

Using PHP 5.3 fgetcsv function, I am experiencing some problems due to encoding matters. Note that that file has spanish "special" latin characters like graphic accents á, é, í ï, etc... I get the CSV file exporting some structured data I have in an…
ElPiter
  • 4,046
  • 9
  • 51
  • 80
14
votes
6 answers

Java API to convert Array to CSV

Suppose I have an array of int, float, string etc. Is there any utility API (e.g. Commons, Guava) that will give me a comma separated string? Like so, int[] a = {1,2,3,4,5}. String s = magicAPI.getCSV(a); // s == "1,2,3,4,5";
Anand Hemmige
  • 3,593
  • 6
  • 21
  • 31
14
votes
3 answers

Reports in Codeigniter

What is the most simplist way to generate reports in Codeigniter framework? Is there any library available to do this task? Except charting what are the other resources to do this.
Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103
14
votes
6 answers

Handling extra newlines (carriage returns) in csv files parsed with Python?

I have a CSV file that has fields that contain newlines e.g.: A, B, C, D, E, F 123, 456, tree , very, bla, indigo (In this case third field in the second row is "tree\n" I tried the following: import csv catalog = csv.reader(open('test.csv', 'rU'),…
mo5470
  • 937
  • 3
  • 10
  • 26
14
votes
4 answers

fputcsv display leading zeros

I'm using a PHP script to generate an excel CSV file from a result-set query. All works fine but when I read my excel file, I can not display leading zeros. This is my code: $rows = $this->Query($sql); $filename =…
jack.cap.rooney
  • 1,306
  • 3
  • 21
  • 37
14
votes
2 answers

How to use GnuPlot to plot a time series chart from a CSV file date and time stored in separate columns?

Lets' take this as the data file: 2012-06-01, 01:00, 1 2012-06-01, 02:00, 2 2012-06-01, 03:00, 4 2012-06-01, 04:00, 3 ... 2012-06-02, 01:00, 5 2012-06-02, 02:00, 2 2012-06-02, 03:00, 1 2012-06-02, 04:00, 1 ... I know how to set timefmt and xdata to…
Ivan
  • 63,011
  • 101
  • 250
  • 382
14
votes
3 answers

Exporting CSV properly open Office (save numbers as TEXT)

I am using OPEN-OFFICE to work and save CSV. I am using the comma delimiter and ' " ' also. However, when saving the CSV, all numbers are not encapasulated. How can I force Open-Office to treat numbers as a Text and have them encapsulated too. …
user1023021
  • 343
  • 4
  • 8
  • 13
13
votes
5 answers

split large csv text file based on column value

I have CSV files that have multiple columns that are sorted. For instance, I might have lines like…
user788171
  • 16,753
  • 40
  • 98
  • 125
13
votes
5 answers

Add rows to CSV File in powershell

Trying to figure out how to add a row to a csv file with titles. I get the content using: $fileContent = Import-csv $file -header "Date", "Description" $File returns Date,Description Text1,text2 text3,text4 How do I append a row with a new date…
Ken
  • 131
  • 1
  • 1
  • 3
13
votes
7 answers

how to get data to javascript from php using json_encode?

I am trying to map traceroutes to google maps. I have an array in php with traceroute data as $c=ip,latitude,longitude, 2nd ip, its latitude, longitude, ....target ip, its lat, its lng I used json_encode($c, JSON_FORCE_OBJECT) and saved the…
user494461
13
votes
3 answers

How to change the encoding during CSV parsing in Rails

I would like to know how can I change the encoding of my CSV file when I import it and parse it. I have this code: csv = CSV.parse(output, :headers => true, :col_sep => ";") csv.each do |row| row = row.to_hash.with_indifferent_access …
TW147
  • 561
  • 2
  • 7
  • 20
13
votes
7 answers

How to read \" double-quote escaped values with read.table in R

I am having trouble to read a file containing lines like the one below in R. "_:b5507F4C7x59005","Fabiana D\"atri" Any idea? How can I make read.table understand that \" is the escape of quote? Cheers, Alexandre
Alexandre Rademaker
  • 2,683
  • 2
  • 19
  • 21