Python CSV Learning

Source: Internet
Author: User

13.1.CSV—CSVFile Reading and Writing

       the so-called CSV (Comma separated Values) format is the most common Import and export format for spreadsheets and databases.  there is no ' CSV standard ', so the format was operationally defined by the many applications which read and write it.  the lack of a standard means, subtle differences often exist in the data produced and consumed by different app Lications.  these differences can make it annoying to process CSV files from multiple sources.  still, while the delimiters and quoting characters vary, the overall format was similar enough that it was possible to Write a single module which can efficiently manipulate such data, hiding the details of reading and writing the data from The programmer.

The so-called CSV (Comma Separated Values) format is the most common import and export format for spreadsheets and databases. There is no "CSV standard", so the format is defined by many application operations that read and write it. This lack of standards means that subtle differences often exist in different application production and consumption data. These differences can make it difficult to process CSV files from multiple sources. At the same time, the delimiter and the reference character change, the overall format is so similar that the programmer can write a separate module, this module can effectively manipulate such data, and encapsulate the details of reading and writing data.

The CSV module implements classes to read and write tabular data in CSVformat. It allows programmers to say, "write this data in the format preferred by Excel," or "read data from this file which is G  Enerated by Excel, "without knowing the precise details of the CSV format used by Excel. Programmers can also describe the CSV formats understood by other applications or define their own special-purpose CSV for Mats.

The CSV module implements table data that reads and writes in CSV format. It allows programmers to say, "Excel writes data in this format," or "reads data from files like Excel," does not know the exact details of the CSV format used by Excel, and programmers can use other applications to describe the CSV format, or to define their own proprietary CSV format.

The CSV module ' s reader and writer objects read and write sequences. Programmers can also read and write data in dictionary form using the dictreader and dictwriter classes.

The reader-writer object of the CSV module can read and write sequences. Programmers can also use the Dictreader and Dictwriter classes to read and write data in the form of dictionaries.

Note:this version of the CSV module doesn ' t support Unicode input.  Also, there is currently some issues regarding ASCII NUL characters. Accordingly, all input should is UTF-8 or printable ASCII to is safe; See the examples inch examples.

Note: The CSV module for this version (2.7) does not support Unicode input. In addition, there are currently some questions about ASCII null characters. Therefore, all inputs should be UTF-8 or print-safe ASCII, which can be seen in the Example section.

13.1.1. Module Contents

The CSV module defines the following functions:

  • csv. Reader (csvfile, dialect= ' Excel ', **fmtparams)

  • Return a Reader object which'll iterate over lines in the given csvfile. csvfileCan is any object which supports theiteratorProtocol and returns a string each time itsNext ()Method is Called-file objects and lists objects are both suitable. If csvfileis a file object, it must being opened with the ' B ' flag in platforms where that makes a difference. An optionaldialectParameter can given which is used to define a set of parameters specific to a particularCSVDialect. It is a instance of a subclass of thedialectClass or one of the strings returned by thelist_dialects ()function. The other optionalFmtparamsKeyword arguments can be given to override individual formatting parameters in the current dialect. For full details about the dialect and formatting parametersdialects and formatting Parameters.

Returns a Read object that iterates through the given csvfile (which can be any object that supports the iterator protocol, the file and List objects are appropriate), and each time the next () method call returns a string. If CSVFile is a file object, it must open the file in "B" mode on a different platform. An optional dialect parameter is commonly used to define a series of specific CSV dialect (not clear why translation is good) parameters. It can be a subclass in the dialect class or an instance of one of the strings returned by the List_dialects () function. Other Optional keyword Parameters fmtparams can be given when the current dialect overrides an individual format parameter. For dialect and formatting parameter details, refer to dialects and formatting Parameters section.

Each row read from the CSV file is returned as a list of strings. No Automatic data type conversion is performed.

A Short Usage Example:

>>>

 >>> import csv>>> with open (' eggs.csv ',  ' RB ')  as  Csvfile:...     spamreader = csv.reader (csvfile, delimiter= '   ',  quotechar= ' | ') ...     for row in spamreader:...          print  ',  '. Join (ROW) spam, spam, spam, spam, spam, baked  beansspamlovely spam, wonderful spam 

Changed in version 2.5: The parser is now stricter with respect to multi-line quoted fields. Previously, if a line ended within a quoted field without a terminating newline character, a newline would is inserted int o The returned field. This behavior caused problems when reading files which contained carriage return characters within fields. The behavior is changed to return the field without inserting newlines. As a consequence, if newlines embedded within fields is important, the input should is split into lines in a manner which Preserves the newline characters.

Changes in version 2.5: The interpreter is now more restrictive than the multi-line quote field. Before, if a line ends without terminating the newline character, the newline character is inserted into the return field. This situation often occurs when a file containing a carriage return character is read. This condition has been modified and the return field is not inserted into the new row. Therefore, if the new row embedded field is important, the input should be divided into the way in which the newline characters are preserved.


  • csv. Writer (csvfile, dialect= ' Excel ', **fmtparams)

  • Return a Writer object responsible for converting the user's data into delimited strings on the given File-like object. csvfileCan is any object with aWrite ()Method. If csvfileis a file object, it must being opened with the ' B ' flag in platforms where that makes a difference. An optionaldialectParameter can given which is used to define a set of parameters specific to a particularCSVDialect. It is a instance of a subclass of thedialectClass or one of the strings returned by thelist_dialects ()function. The other optionalFmtparamsKeyword arguments can be given to override individual formatting parameters in the current dialect. For full details about the dialect and formatting parametersdialects and formatting Parameters. To make it as easy as possible-interface with modules which implement the DB API, the valueNoneis written as the empty string. While this isn ' t a reversible transformation, it makes it easier to dump SQL NULL data values toCSVFiles without preprocessing the data returned from acursor.fetch*Call. All other non-string data is stringified withstr ()Before being written.

    Not to be continued ...


Python CSV Learning

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.