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

Read in the first column of a CSV in Python

I have a CSV (mylist.csv) with 2 columns that look similar to this: jfj840398jgg item-2f hd883hb2kjsd item-9k jie9hgtrbu43 item-12 fjoi439jgnso item-3i I need to read the first column into a variable so I just…
P.J.
  • 217
  • 2
  • 4
  • 13
13
votes
1 answer

How to skip an unknown number of empty lines before header on pandas.read_csv?

I want to read a dataframe from a csv file where the header is not in the first line. For example: In [1]: import pandas as pd In [2]: import io In [3]: temp=u"""#Comment 1 ...: #Comment 2 ...: ...: #The previous line is empty ...:…
bmello
  • 1,864
  • 3
  • 18
  • 23
13
votes
2 answers

Ruby on Rails CSV putting "" instead of actual quotes

I am attempting to generate a CSV file. Everything is fine except for blank fields, I'm not quite sure have "" instead of actual quotes. I've provided the code I'm using to generate the file and some output. <% headers = ["Username",…
Adam Leonard
  • 133
  • 1
  • 5
13
votes
3 answers

COPY column order

I'm trying to use COPY with HEADER option but my header line in file is in different order than the column order specified in database. Is the column name order necessary in my file ?? My code is as below: COPY table_name ( …
sneha
  • 169
  • 1
  • 4
  • 11
13
votes
5 answers

Python import CSV short code (pandas?) delimited with ';' and ',' in entires

I need to import a CSV file in Python on Windows. My file is delimited by ';' and has strings with non-English symbols and commas (','). I've read posts: Importing a CSV file into a sqlite3 database table using Python Python import csv to list When…
Alex Martian
  • 3,423
  • 7
  • 36
  • 71
13
votes
2 answers

How to read contents of a csv file inside zip file using PowerShell

I have a zip file which contains several CSV files inside it. How do I read the contents of those CSV files without extracting the zip files using PowerShell? I having been using the Read-Archive Cmdlet which is included as part of the PowerShell…
Ishan
  • 3,931
  • 11
  • 37
  • 59
13
votes
5 answers

Writing to a particular cell using csv module in python

I have to write a value to a particular cell (say the 8th cell) in my csv file. I can see there is a csvwriter.writerow(row) method to write an entire row, but I am not seeing anything to write a value to a particular cell.
sagar
  • 1,375
  • 5
  • 20
  • 38
13
votes
4 answers

jackson serialize csv property order

we have a table with 350+ columns. pojo class is generated and getters order get messed up. trying to use csvmapper from jackson, but it generates csv based on getter order. @JsonPropertyOrder is also not use feasible because of many columns.we…
user2598799
  • 141
  • 1
  • 1
  • 7
13
votes
3 answers

Java - Write CSV File with Apache.commons.csv

I'm using the apache.commons.csv library in Java. I'm reading a CSV file from a web page with this code: InputStream input = new URL(url).openStream(); Reader reader = new InputStreamReader(input, "UTF-8"); defaultParser = new…
jonbon
  • 1,142
  • 3
  • 12
  • 37
13
votes
3 answers

python read csv file with row and column headers into dictionary with two keys

I have csv file of the following format, ,col1,col2,col3 row1,23,42,77 row2,25,39,87 row3,48,67,53 row4,14,48,66 I need to read this into a dictionary of two keys such that dict1['row1']['col2'] = 42 dict1['row4']['col3'] = 66 If I try to use…
rambalachandran
  • 2,091
  • 2
  • 19
  • 34
13
votes
7 answers

How to read the csv file properly if each row contains different number of fields (number quite big)?

I have a text file from amazon, containing the following info: # user item time rating review text (the header is added by me for explanation, not in the text file disjiad123 TYh23hs9 13160032 5 I love this…
user5779223
  • 1,460
  • 3
  • 21
  • 42
13
votes
1 answer

How to escape a comma in CSV file?

I have string which contains "," and need to serialize/deserialize it as CSV. i've been able to serialize it correctly, but when deserialize, it always wrong. here is my code in java List header = ... test1, test2, test3 <-- string contains…
user468587
  • 4,799
  • 24
  • 67
  • 124
13
votes
2 answers

How to read index data as string with pandas.read_csv()?

I'm trying to read csv file as DataFrame with pandas, and I want to read index row as string. However, since the row for index doesn't have any characters, pandas handles this data as integer. How to read as string? Here are my csv file and…
ykensuke9
  • 714
  • 2
  • 7
  • 15