Python Read and Write Csv Files 2091 Lyzoyj

2
 PYTHON - Read and Write CSV files June 2014  [PYTHON] Read and Write CSV files It is possible to read and wri te CSV (comma separated values) fil es using Python 2.4 Distribution. Like most languages, file operations can be done with Python. Writ ing a CSV fil e with Python can be done by importing the CSV module and creating a write object that will be used wi th the Writ eRow Method. Reading a CS V fil e can be done in a simila r way by creating a reader object and by using the print method to read the file. As file operations require advanced concepts, some knowledge of programming with Python i s required to read and write the CSV (comma separated values) files. Prerequisites Wr iting a CSV file Reading a CSV file Python Www.python.org, version 2.4 supports the de facto  CSV  format (comma-separated values: ). The Reference Library is very helpful when l earning to use Python. Here are some additional tips to get you started. Prerequisites

description

Python Read and Write Csv Files 2091 Lyzoyj

Transcript of Python Read and Write Csv Files 2091 Lyzoyj

  • PYTHON - Read and Write CSV filesJune 2014

    [PYTHON] Read and Write CSV filesIt is possible to read and write CSV (comma separated values) files using Python 2.4Distribution. Like most languages, file operations can be done with Python. Writing a CSV filewith Python can be done by importing the CSV module and creating a write object that will beused with the WriteRow Method. Reading a CSV file can be done in a similar way by creating areader object and by using the print method to read the file. As file operations require advancedconcepts, some knowledge of programming with Python is required to read and write the CSV(comma separated values) files.

    PrerequisitesWriting a CSV fileReading a CSV file

    Python Www.python.org, version 2.4 supports the de facto CSV format (comma-separatedvalues: ). The Reference Library is very helpful when learning to use Python. Here are someadditional tips to get you started.

    Prerequisites

  • -> Knowledge of Python -> Python 2.4 Distribution

    Writing a CSV fileStart by importing the CSV module:import csvWe 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 row. Writerow The method takes one argument - thisargument must be a list and each list item is equivalent to a column. Here, we try to make anaddress book:c.writerow(["Name","Address","Telephone","Fax","E-mail","Others"])Then we will save all entries in this way.

    Reading a CSV fileFirst just create an object reader (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 rowWe can of course extract a precise entry of a row with the index:for row in reader: print row[2], row[-2]

    This document entitled PYTHON - Read and Write CSV files from Kioskea (en.kioskea.net) is made availableunder the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated bythe license, as this note appears clearly.