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

Using CSVHelper on file upload

I am trying to use the CSVhelper plugin to read an uploaded CSV file. Here is my modelBinder class: public class SurveyEmailListModelsModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext,…
user547794
  • 14,263
  • 36
  • 103
  • 152
12
votes
2 answers

How to use multiple delimiters when using csv.reader in python?

I have multiple lines of texts in a text file that look similar to this: 2012-03-16 13:47:30.465 -0400 START Running Lab.script 19 on_the I want to be able to convert this text file into csv. I've already done that using this code: fin…
user1186173
  • 565
  • 3
  • 7
  • 26
12
votes
1 answer

MySQL load dates in mm/dd/yyyy format

I've got a MySQL load script that almost works, it is perfect except for the date columns, which are not in a MySql friendly format. load data infile '/Users/pfarrell/sandbox/waybase/folklore/Titles_1976.csv' into table fix76 fields terminated by…
fishtoprecords
  • 2,394
  • 7
  • 27
  • 38
12
votes
4 answers

Linux - join 2 CSV files

I have 2 CSV files: file_1 columns: id,user_id,message_id,rate file_2 columns: id,type,timestamp The relation between the files is that file_1.message_id = files_2.id. I want to create a 3rd file that will have the following…
Ran
  • 3,455
  • 12
  • 47
  • 60
12
votes
1 answer

How to escape commas inside CSV values when importing table to MySQL?

I am trying to import a CSV file into a MySQL database table. The CSV rows look like the following: "00602","…
TKpop
  • 309
  • 1
  • 4
  • 8
12
votes
3 answers

How can I prevent csv.DictWriter() or writerow() rounding my floats?

I have a dictionary that I want to write to a csv file, but the floats in the dictionary are rounded off when I write them to the file. I want to keep the maximum precision. Where does the rounding occur and how can I prevent it? What I did I…
aDroid
  • 1,008
  • 1
  • 10
  • 12
12
votes
5 answers

How to create a separate CSV file from VBA?

I need to output some results as a .csv file, that gets parsed later on by another process. In order to produce these results, I have a huge workbook containing all the macros and functions that I need. Is it possible to "create" a separate .csv…
BuZz
  • 16,318
  • 31
  • 86
  • 141
12
votes
3 answers

Tab delimited file parsing in Rails

I have a file that looks like this ID Name Car 1 Mike Honda 2 Adam Jim These values are tab delimited, and from this I want to parse it in Ruby and put it into my database. I have tried the following require…
Mike Silvis
  • 1,299
  • 2
  • 17
  • 30
12
votes
7 answers

Importing CSV data into C# classes

I know how to read and display a line of a .csv file. Now I would like to parse that file, store its contents in arrays, and use those arrays as values for some classes I created. I'd like to learn how though. Here is an…
lorenzoid
  • 1,812
  • 10
  • 33
  • 51
12
votes
2 answers

Python Scrapy: How to get CSVItemExporter to write columns in a specific order

In Scrapy, I have my items specified in a certain order in items.py, & my spider has those items again in the same order. However, when I run the spider & save the results as a csv, the column order from the items.py or the spider is not maintained.…
user818190
  • 579
  • 2
  • 11
  • 21
12
votes
3 answers

Export gridview data into CSV file

I have a gridview control in ASP.Net 2.0 and i need to export this gridview data into CSV file. I have bind this gridview with the dataset.After binding the dataset to the gridview i have done some changes in the gridview data like if I got 0 in…
jitendra
  • 567
  • 4
  • 11
  • 17
12
votes
7 answers

Removing multiple delimiters between outside delimiters on each line

Using awk or sed in a bash script, I need to remove comma separated delimiters that are located between an inner and outer delimiter. The problem is that wrong values ends up in the wrong columns, where only 3 columns are desired. For example, I…
A.J. Hart
  • 143
  • 6
12
votes
7 answers

How can I parse quoted CSV in Perl with a regex?

I'm having some issues with parsing CSV data with quotes. My main problem is with quotes within a field. In the following example lines 1 - 4 work correctly but 5,6 and 7…
Mark Nold
  • 5,638
  • 7
  • 31
  • 33
12
votes
4 answers

how to import CSV using zend

How do I import CSV files using zend framework? Should I use zend_file_transfer or is there any special class that I have to look into? Also if I use zend_file_transfer is there any special validator for CSV?
JABD
  • 640
  • 2
  • 10
  • 23
12
votes
6 answers

How to Access Private Github Repo File (.csv) in Python using Pandas or Requests

I had to switch my public Github repository to private and cannot access files, not with access tokens that I was able to with the public Github repo. I can access my private repo's CSV with curl: ''' curl -s…
everwitt7
  • 191
  • 1
  • 3
  • 12