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

How to parse CSV data?

Where could I find some JavaScript code to parse CSV data?
Pierre-Gilles Levallois
  • 4,161
  • 4
  • 28
  • 37
273
votes
8 answers

Can a CSV file have a comment?

Is there any official way to allow a CSV formatted file to allow comments, either on its own line OR at the end of a line? I tried checking wikipedia on this and also RFC 4180 but both do not mention anything which leads me to believe that it's not…
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
265
votes
13 answers

Python import csv to list

I have a CSV file with about 2000 records. Each record has a string, and a category to it: This is the first line,Line1 This is the second line,Line2 This is the third line,Line3 I need to read this file into a list that looks like this: data =…
MorganTN
  • 2,817
  • 3
  • 14
  • 10
264
votes
6 answers

How to export query result to csv in Oracle SQL Developer?

I'm using Oracle SQL Developer 3.0. Trying to figure out how to export a query result to a text file (preferably CSV). Right clicking on the query results window doesn't give me any export options.
Ken Liu
  • 22,503
  • 19
  • 75
  • 98
262
votes
16 answers

Writing data into CSV file in C#

I am trying to write into a csv file row by row using C# language. Here is my function string first = reader[0].ToString(); string second=image.ToString(); string csv = string.Format("{0},{1}\n", first, second); File.WriteAllText(filePath,…
rampuriyaaa
  • 4,926
  • 10
  • 34
  • 41
261
votes
8 answers

How do I convert this list of dictionaries to a csv file?

I have a list of dictionaries that looks something like this: toCSV = [{'name':'bob','age':25,'weight':200},{'name':'jim','age':31,'weight':180}] What should I do to convert this to a csv file that looks something like…
backus
  • 4,076
  • 5
  • 28
  • 30
251
votes
13 answers

Create a .csv file with values from a Python list

I am trying to create a .csv file with the values from a Python list. When I print the values in the list they are all unicode (?), i.e. they look something like this [u'value 1', u'value 2', ...] If I iterate through the values in the list i.e.…
Fortilan
  • 2,645
  • 2
  • 17
  • 10
248
votes
12 answers

Writing a Python list of lists to a csv file

I have a long list of lists of the following form --- a = [[1.2,'abc',3],[1.2,'werew',4],........,[1.4,'qew',2]] i.e. the values in the list are of different types -- float,int, strings.How do I write it into a csv file so that my output csv file…
user1403483
245
votes
11 answers

Python csv string to array

Anyone know of a simple library or function to parse a csv encoded string and turn it into an array or dictionary? I don't think I want the built in csv module because in all the examples I've seen that takes filepaths, not strings.
Drew LeSueur
  • 19,185
  • 29
  • 89
  • 106
242
votes
6 answers

Pandas read_csv from url

I'm trying to read a csv-file from given URL, using Python 3.x: import pandas as pd import requests url = "https://github.com/cs109/2014_data/blob/master/countries.csv" s = requests.get(url).content c = pd.read_csv(s) I have the following…
venom
  • 2,563
  • 2
  • 11
  • 8
238
votes
14 answers

Import CSV file into SQL Server

I am looking for help to import a .csv file into SQL Server using BULK INSERT and I have few basic questions. Issues: The CSV file data may have , (comma) in between (Ex: description), so how can I make import handling these data? If the client…
Prabhat
  • 4,164
  • 4
  • 17
  • 12
233
votes
6 answers

datetime dtypes in pandas read_csv

I'm reading in a csv file with multiple datetime columns. I'd need to set the data types upon reading in the file, but datetimes appear to be a problem. For instance: headers = ['col1', 'col2', 'col3', 'col4'] dtypes = ['datetime', 'datetime',…
user3221055
  • 2,331
  • 2
  • 12
  • 3
232
votes
7 answers

SQLite: How do I save the result of a query as a CSV file?

Is there a way I can export the results of a query into a CSV file?
RayLoveless
  • 19,880
  • 21
  • 76
  • 94
229
votes
9 answers

How to load a tsv file into a Pandas DataFrame?

I'm trying to get a tsv file loaded into a pandas DataFrame. This is what I'm trying and the error I'm getting: >>> df1 = DataFrame(csv.reader(open('c:/~/trainSetRel3.txt'), delimiter='\t')) Traceback (most recent call last): File "",…
screechOwl
  • 27,310
  • 61
  • 158
  • 267
228
votes
12 answers

Ruby on Rails - Import Data from a CSV file

I would like to import data from a CSV file into an existing database table. I do not want to save the CSV file, just take the data from it and put it into the existing table. I am using Ruby 1.9.2 and Rails 3. This is my table: create_table…
freshest
  • 6,229
  • 9
  • 35
  • 38