Questions tagged [csv]

Comma-Separated Values or Character-Separated Values (CSV) is a standard "flat file database" format for storing tabular data in plain text, consisting of an optional header row that lists the table fields delimited by commas or tabs or other delimiter character, followed by one or more rows (newline separated) representing the table records as delimited lists of the values. Newlines and separator characters can appear within (quoted) fields.

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             |
+-------+-------------+----------+----------------------+

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

In this example, the first row of CSV data serves as the "header" which describes the corresponding data below it. Each successive line of the CSV file would then neatly fit into the same field as the first line. There is no inherent way to describe within a CSV file whether the first row is a header row or not.

Note that 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.

Questions tagged are expected to relate to programming in some way, for example, parsing/importing CSV files or creating them programmatically.

Related links:

77596 questions
217
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,327
  • 3
  • 10
  • 10
211
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
  • 5,769
  • 8
  • 30
  • 38
209
votes
10 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
  • 16,653
  • 27
  • 85
  • 96
208
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,205
  • 2
  • 15
  • 10
207
votes
8 answers

How to get rid of "Unnamed: 0" column in a pandas DataFrame?

I have a situation wherein sometimes when I read a csv from df I get an unwanted index-like column named unnamed:0. file.csv ,A,B,C 0,1,2,3 1,4,5,6 2,7,8,9 The CSV is read with this: pd.read_csv('file.csv') Unnamed: 0 A B C 0 0 …
Michael Perdue
  • 5,475
  • 13
  • 32
  • 53
207
votes
12 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
  • 3,824
  • 3
  • 15
  • 12
202
votes
4 answers

How to add header row to a pandas DataFrame

I am reading a csv file into pandas. This csv file constists of four columns and some rows, but does not have a header row, which I want to add. I have been trying the following: Cov = pd.read_csv("path/to/file.txt",…
sequence_hard
  • 3,763
  • 9
  • 24
  • 46
200
votes
30 answers

How can I output a UTF-8 CSV in PHP that Excel will read properly?

I've got this very simple thing that just outputs some stuff in CSV format, but it's got to be UTF-8. I open this file in TextEdit or TextMate or Dreamweaver and it displays UTF-8 characters properly, but if I open it in Excel it's doing this silly…
Ben Saufley
  • 2,958
  • 5
  • 25
  • 39
200
votes
4 answers

Properly escape a double quote in CSV

I have a line like this in my CSV: "Samsung U600 24"","10000003409","1","10000003427" Quote next to 24 is used to express inches, while the quote just next to that quote closes the field. I'm reading the line with fgetcsv but the parser makes a…
srgb
  • 3,785
  • 6
  • 22
  • 44
198
votes
11 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
197
votes
23 answers

How to use mongoimport to import csv

Trying to import a CSV with contact information: Name,Address,City,State,ZIP Jane Doe,123 Main St,Whereverville,CA,90210 John Doe,555 Broadway Ave,New York,NY,10010 Running this doesn't seem to add any documents to the database: $ mongoimport…
Joe
  • 21,593
  • 12
  • 36
  • 35
196
votes
7 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
  • 3,327
  • 5
  • 25
  • 29
195
votes
5 answers

TypeError: a bytes-like object is required, not 'str' in python and CSV

TypeError: a bytes-like object is required, not 'str' getting above error while Executing below python code to save the HTML table data in Csv file. don't know how to get rideup.pls help me. import csv import requests from bs4 import…
ShivaGuntuku
  • 3,635
  • 4
  • 23
  • 34
193
votes
7 answers

How to correctly display .csv files within Excel 2013?

It seems Excel 2013 doesn't read CSV files correctly (Excel 2010 does). Every time I open .csv files, all my data are displayed in the first column. I know I can go to DATA, Convert, and then choose commas, but this set up is not saved, and I am fed…
David
  • 4,490
  • 6
  • 31
  • 54
192
votes
6 answers

Output array to CSV in Ruby

It's easy enough to read a CSV file into an array with Ruby but I can't find any good documentation on how to write an array into a CSV file. Can anyone tell me how to do this? I'm using Ruby 1.9.2 if that matters.
Jason Swett
  • 38,405
  • 60
  • 193
  • 322