[PYTHON]Read and Write CSV files

Last update on July 19, 2009 10:35 AM by jak58
Published by jad05

[PYTHON] Read and Write CSV files








Python Www.python.org, version 2.4 supports the de facto CSV format (comma-separated values: Comma Separated Values).

The Reference Library is very explanatory in this regard, but only in English.

Here is how to read and write CSV with Python.

Prerequisite


Nothing really impossible:

-> Knowledge of Python
-> Python 2.4 Distribution

Writing a CSV file


Let's start by importing the CSV module:
import csv

We will define an object "writer" (named c), which can later be used to write the CSV file.

c = csv.writer(open("MYFILE.csv", "wb"))


Now we will apply the method to write a writerow row. Writerow The method takes one argument: this argument must be a list and each list item is equivalent to a column. Here, we try to make an address book.

c.writerow(["Name","Address","Telephone","Fax","E-mail","Others"])


Then we will save all entries in this way.

Reading a CSV file


First just create and objectnreader (we will give it a name: cr).

cr = csv.reader(open("MYFILE.csv","rb"))


And here we get each row (in the form of a list of columns) as follows:

for row in cr:
    print row



We can of course extract a precise entry of a row with the index (as a list but a list).


for row in reader:
    print row[2], row[-2]
Best answers for « Read and Write CSV files » in :
Read/ write a file .mdf/.mds Show Read/ write a file .mdf/.mds MDF file is an ISO image file created with the software Alcohol 120 degrees. The .Mdf must be combined with a file .Mds for providing information on the audio CD or DVD to be burned. Download Alcohol...
Read or write a file .IMG/ .CCD/ .Sub Show Read or write a file .IMG/ .CCD/ .Sub A File containing the respective format .Img .Ccd and .Sub: The .IMG is the CD image, ie a binary copy of a CD or DVD in the form of a file and .ccd and .sub contain information about the...
Reading a .Pub file without Publisher ShowReading a .Pub file without Publisher Basically it is impossible. The aim is to: Simply convert the .Pub to .Pdf. Here is a link, for a website on which you can make this conversion for free: http://www.conv2pdf.com/ Just...
How to Open/Read a DVI file format on Windows? ShowHow to Open/Read a DVI file format on Windows? DVI is a format description document independent media (like PDF). (DVI Device Independent =) It is generally created from LaTeX file. Tex On Windows, you can install MiKTeX, which...
Nintendo DS: Failed to write a file to your Action Replay ShowNintendo DS: Failed to write a file to your Action Replay Introduction How to reset Action Replay Re-installation of games set as default under Action Replay Introduction Action Replay is a tool which can control the behaviors of...
Download 3GP Player 2008 ShowDescription The application is designed Reganam Interactive. 3GP Player is an application that allows you to convert your 3GP files in a compatible format on your PC. It enables you to read your 3GP files also. The application reads the following...
Download Locked Files Wizard (ex CopyLock) ShowA program, a folder or a fil used by a process can't be moved or erased. Locked Files Wizard (LFW) is a wizard allowing to move, delete or rename a file or a folder, even if it's used by a process. This program also allows to stop running process...
UNIX - Files ShowIntroduction to UNIX files In UNIX systems any element is represented in the form of a file. All files are architectured around a single tree structure where the base, called the root, is written "/". File types UNIX systems define different...
UNIX Commands ShowTable of the main UNIX commands Unix Commands Description Options ls lists the content of a directory -a Displays all files, including hidden files -I Displays a...
Linux - The shell ShowIntroduction to the shell The command interpreter is the interface between the user and the operating system, hence the name "shell". The shell therefore acts as an intermediary between the operating system and the user thanks to command lines...