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

PowerShell: Import-CSV with no headers and remove partial duplicate lines

I have a log file that is formatted as a CSV with no headers. The first column is basically the unique identifier for the issues being recorded. There may be multiple lines with different details for the same issue identifier. I would like to remove…
Joshua Nurczyk
  • 325
  • 2
  • 4
  • 14
14
votes
3 answers

Python: How do I use DictReader twice?

This feels like a very basic question, but I can't find any mention of it elsewhere. I'm a beginning Python user. When I read in data using DictReader, and then use the dictionary, I'm unable to reference it again. For example, using this…
NickCHK
  • 1,093
  • 7
  • 17
14
votes
8 answers

Convert CSV values to a HashMap key value pairs in JAVA

HI I have a csv called test.csv . I am trying to read the csv line by line and convert the values into a hash key value pairs . Here is the code :- public class Example { public static void main(String[] args) throws ParseException, IOException { …
Newbie
  • 2,664
  • 7
  • 34
  • 75
14
votes
4 answers

Why is R reading UTF-8 header as text?

I saved an Excel table as text (*.txt). Unfortunately, Excel don't let me choose the encoding. So I need to open it in Notepad (which opens as ANSI) and save it as UTF-8. Then, when I read it in R: data <-…
Rodrigo
  • 4,706
  • 6
  • 51
  • 94
14
votes
3 answers

Building list of lists from CSV file

I have an Excel file(that I am exporting as a csv) that I want to parse, but I am having trouble with finding the best way to do it. The csv is a list of computers in my network, and what accounts are in the local administrator group for each one. I…
Michael Dornisch
  • 177
  • 1
  • 2
  • 8
14
votes
3 answers

What's the easiest way to get the headers from a CSV file in Ruby?

All I need to do is get the headers from a CSV file. file.csv is: "A", "B", "C" "1", "2", "3" My code is: table = CSV.open("file.csv", :headers => true) puts table.headers table.each do |row| puts row end Which gives me: true "1", "2",…
Anthony To
  • 2,193
  • 2
  • 21
  • 29
14
votes
2 answers

Represent a tree hierarchy using an Excel spreadsheet to be easily parsed by Python CSV reader?

I have a non-technical client who has some hierarchical product data that I'll be loading into a tree structure with Python. The tree has a variable number of levels, and a variable number nodes and leaf nodes at each level. The client already…
Erich
  • 2,509
  • 2
  • 21
  • 24
14
votes
3 answers

Viewing delimited columns in emacs

Is anyone aware of an emacs mode or function that will reformat a buffer holding a delimited file such that each delimiter (e.g. tab) defines a "column" with the width of each column set to the longest entry? I can reset the tab-width variable but…
Alex Stoddard
  • 8,244
  • 4
  • 41
  • 61
14
votes
4 answers

VBScript to loop through all files in a folder

I have the code to carry out the process on a single file, could anyone alter this script so it loops through all files in the directory "H:\Letter Display\Letters" with the file type ".LTR" and saves them all: Const ForReading = 1 Const…
Nathan Hawthorne
  • 303
  • 3
  • 9
  • 26
14
votes
3 answers

Convert a numpy array to a CSV string and a CSV string back to a numpy array

I have to convert a numpy array of floats to a string (to store in a SQL DB) and then also convert the same string back into a numpy float array. This is how I'm going to a string (based on this article) VIstring = ''.join(['%.5f,' % num for num in…
Dan
  • 45,079
  • 17
  • 88
  • 157
14
votes
6 answers

Dealing with fields containing unescaped double quotes with TextFieldParser

I am trying to import a CSV file using TextFieldParser. A particular CSV file is causing me problems due to its nonstandard formatting. The CSV in question has its fields enclosed in double quotes. The problem appears when there is an additional…
sglantz
  • 2,063
  • 4
  • 20
  • 30
14
votes
5 answers

How to write only selected class fields into CSV with CsvHelper?

I use CsvHelper to read and write CSV files and it is great, yet I don't understand how to write only selected type fields. Say we had: using CsvHelper.Configuration; namespace Project { public class DataView { [CsvField(Name =…
myWallJSON
  • 9,110
  • 22
  • 78
  • 149
14
votes
7 answers

Reading CSV files in C#

Does anyone know of an open-source library that allows you to parse and read .csv files in C#?
diegocaro
  • 285
  • 1
  • 2
  • 7
14
votes
4 answers

Creating multiple CSV sheets in Python

Is there any way to create a CSV file with multiple sheets programmatically in Python?
ianlokejj
  • 153
  • 1
  • 2
  • 5
14
votes
7 answers

Execute SQL on CSV files via JDBC

I need to apply an SQL query to CSV files (comma-separated text files). My SQL is predefined from another tool, and is not eligible to change. It may contain embedded selects and table aliases in the FROM part. For my task I have found two…
Markos Fragkakis
  • 7,499
  • 18
  • 65
  • 103