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
280
votes
10 answers

(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

I'm trying to read a .csv file into Python (Spyder) but I keep getting an error. My code: import csv data = open("C:\Users\miche\Documents\school\jaar2\MIK\2.6\vektis_agb_zorgverlener") data = csv.reader(data) print(data) I get the following…
Miesje
  • 2,797
  • 3
  • 8
  • 7
277
votes
6 answers

CSV in Python adding an extra carriage return, on Windows

import csv with open('test.csv', 'w') as outfile: writer = csv.writer(outfile, delimiter=',', quoting=csv.QUOTE_MINIMAL) writer.writerow(['hi', 'dude']) writer.writerow(['hi2', 'dude2']) The above code generates a file, test.csv, with…
apalopohapa
  • 4,103
  • 4
  • 23
  • 29
272
votes
11 answers

Keep only date part when using pandas.to_datetime

I use pandas.to_datetime to parse the dates in my data. Pandas by default represents the dates with datetime64[ns] even though the dates are all daily only. I wonder whether there is an elegant/clever way to convert the dates to datetime.date or…
user1642513
272
votes
8 answers

_csv.Error: field larger than field limit (131072)

I have a script reading in a csv file with very huge fields: # example from http://docs.python.org/3.3/library/csv.html?highlight=csv%20dictreader#examples import csv with open('some.csv', newline='') as f: reader = csv.reader(f) for row in…
user1251007
  • 13,241
  • 13
  • 46
  • 73
265
votes
16 answers

Turning a Comma Separated string into individual rows

I have a SQL Table like this: | SomeID | OtherID | Data +----------------+-------------+------------------- | abcdef-..... | cdef123-... | 18,20,22 | abcdef-..... | 4554a24-... | 17,19 | 987654-..... | 12324a2-... | 13,19,20 is…
Michael Stum
  • 167,397
  • 108
  • 388
  • 523
252
votes
14 answers

How to import multiple .csv files at once?

Suppose we have a folder containing multiple data.csv files, each containing the same number of variables but each from different times. Is there a way in R to import them all simultaneously rather than having to import them all individually? My…
Jojo
  • 3,923
  • 7
  • 19
  • 27
250
votes
8 answers

append new row to old csv file python

I am trying to add a new row to my old csv file. Basically, it gets updated each time I run the Python script. Right now I am storing the old csv rows values in a list and then deleting the csv file and creating it again with the new list…
laspal
  • 2,827
  • 3
  • 18
  • 10
248
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
  • 21,056
  • 17
  • 71
  • 96
244
votes
4 answers

Skip the headers when editing a csv file using Python

I am using below referred code to edit a csv using Python. Functions called in the code form upper part of the code. Problem: I want the below referred code to start editing the csv from 2nd row, I want it to exclude 1st row which contains headers.…
user1915050
238
votes
12 answers

Example JavaScript code to parse CSV data

Where could I find some JavaScript code to parse CSV data?
Pierre-Gilles Levallois
  • 3,697
  • 4
  • 23
  • 34
237
votes
15 answers

How do I read a large csv file with pandas?

I am trying to read a large csv file (aprox. 6 GB) in pandas and i am getting a memory error: MemoryError Traceback (most recent call last) in () ----> 1…
Rajkumar Kumawat
  • 2,857
  • 3
  • 10
  • 8
232
votes
7 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
  • 78,923
  • 102
  • 356
  • 586
227
votes
15 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,288
  • 9
  • 31
  • 41
223
votes
19 answers

Create a CSV File for a user in PHP

I have data in a MySQL database. I am sending the user a URL to get their data out as a CSV file. I have the e-mailing of the link, MySQL query, etc. covered. How can I, when they click the link, have a pop-up to download a CVS with the record from…
Jason
221
votes
26 answers

How can I convert JSON to CSV?

I have a JSON file I want to convert to a CSV file. How can I do this with Python? I tried: import json import csv f = open('data.json') data = json.load(f) f.close() f = open('data.csv') csv_file = csv.writer(f) for item in data: …
little_fish
  • 3,099
  • 4
  • 18
  • 8