來源自 這裡
Preface :
New in version 2.3.
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 operationally defined by the many applications which read and write it. The lack of a standard means that subtle differences often exist in the data produced and consumed by different applications. These differences can make it annoying to process CSV files from multiple sources. Still, while the delimiters and quoting characters vary, the overall format is similar enough that it is 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 csv module implements classes to read and write tabular data in CSV format. It allows programmers to say, "write this data in the format preferred by Excel," or "read data from this file which was generated 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 formats.
The csv module’s reader and writer objects read and write sequences. Programmers can also read and write data in dictionary form using the DictReaderand DictWriter classes. For more, please refer to PEP 305 - CSV File API
Note :
Module Contents :
The csv module defines the following functions :
- csv.reader(csvfile[, dialect='excel'][, fmtparam])
- csv.writer(csvfile[, dialect='excel'][, fmtparam])
- csv.register_dialect(name[, dialect][, fmtparam])
- csv.get_dialect(name)
- csv.list_dialects()
- csv.field_size_limit([new_limit])
The csv module defines the following classes :
- class csv.DictReader(csvfile, fieldnames=None, restkey=None, restval=None, dialect='excel', *args, **kwds)
- class csv.DictWriter(csvfile, fieldnames, restval='', extrasaction='raise', dialect='excel', *args, **kwds)
- class csv.Dialect
- class csv.excel
- class csv.excel_tab
- class csv.Sniffer
An example for Sniffer use :
The
csv module defines the following constants :
- csv.QUOTE_ALL
- csv.QUOTE_MINIMAL
- csv.QUOTE_NONNUMERIC
- csv.QUOTE_NONE
The csv module defines the following exception :
- exception csv.Error
Reader Objects :
Reader objects (DictReader instances and objects returned by the reader() function) have the following public methods:
- csvreader.next()
- csvreader.dialect
- csvreader.line_num
DictReader objects have the following public attribute :
- csvreader.fieldnames
Writer Objects :
Writer objects (DictWriter instances and objects returned by the writer() function) have the following public methods. A row must be a sequence of strings or numbers for Writer objects and a dictionary mapping fieldnames to strings or numbers (by passing them through str() first) for DictWriter objects. Note that complex numbers are written out surrounded by parens. This may cause some problems for other programs which read CSV files (assuming they support complex numbers at all).
- csvwriter.writerow(row)
- csvwriter.writerows(rows)
Writer objects have the following public attribute :
- csvwriter.dialect
DictWriter objects have the following public method :
- DictWriter.writeheader()
Examples :
The simplest example of reading a CSV file :
Reading a file with an alternate format :
The corresponding simplest possible writing example is :
Execution sample of writer :
Registering a new dialect :
A slightly more advanced use of the reader — catching and reporting errors :
The csv module doesn’t directly support reading and writing Unicode, but it is 8-bit-clean save for some problems with ASCII NUL characters. So you can write functions or classes that handle the encoding and decoding for you as long as you avoid encodings like UTF-16 that use NULs. UTF-8 is recommended.
Supplement :
* Dialects and Formatting Parameters
Preface :
New in version 2.3.
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 operationally defined by the many applications which read and write it. The lack of a standard means that subtle differences often exist in the data produced and consumed by different applications. These differences can make it annoying to process CSV files from multiple sources. Still, while the delimiters and quoting characters vary, the overall format is similar enough that it is 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 csv module implements classes to read and write tabular data in CSV format. It allows programmers to say, "write this data in the format preferred by Excel," or "read data from this file which was generated 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 formats.
The csv module’s reader and writer objects read and write sequences. Programmers can also read and write data in dictionary form using the DictReaderand DictWriter classes. For more, please refer to PEP 305 - CSV File API
Note :
Module Contents :
The csv module defines the following functions :
- csv.reader(csvfile[, dialect='excel'][, fmtparam])
- csv.writer(csvfile[, dialect='excel'][, fmtparam])
- csv.register_dialect(name[, dialect][, fmtparam])
- csv.get_dialect(name)
- csv.list_dialects()
- csv.field_size_limit([new_limit])
The csv module defines the following classes :
- class csv.DictReader(csvfile, fieldnames=None, restkey=None, restval=None, dialect='excel', *args, **kwds)
- class csv.DictWriter(csvfile, fieldnames, restval='', extrasaction='raise', dialect='excel', *args, **kwds)
- class csv.Dialect
- class csv.excel
- class csv.excel_tab
- class csv.Sniffer
An example for Sniffer use :
- csvfile = open("example.csv", "rb")
- dialect = csv.Sniffer().sniff(csvfile.read(1024))
- csvfile.seek(0)
- reader = csv.reader(csvfile, dialect)
- # ... process CSV file contents here ...
- csv.QUOTE_ALL
- csv.QUOTE_MINIMAL
- csv.QUOTE_NONNUMERIC
- csv.QUOTE_NONE
The csv module defines the following exception :
- exception csv.Error
Reader Objects :
Reader objects (DictReader instances and objects returned by the reader() function) have the following public methods:
- csvreader.next()
- csvreader.dialect
- csvreader.line_num
DictReader objects have the following public attribute :
- csvreader.fieldnames
Writer Objects :
Writer objects (DictWriter instances and objects returned by the writer() function) have the following public methods. A row must be a sequence of strings or numbers for Writer objects and a dictionary mapping fieldnames to strings or numbers (by passing them through str() first) for DictWriter objects. Note that complex numbers are written out surrounded by parens. This may cause some problems for other programs which read CSV files (assuming they support complex numbers at all).
- csvwriter.writerow(row)
- csvwriter.writerows(rows)
Writer objects have the following public attribute :
- csvwriter.dialect
DictWriter objects have the following public method :
- DictWriter.writeheader()
Examples :
The simplest example of reading a CSV file :
Reading a file with an alternate format :
- import csv
- with open('passwd', 'rb') as f:
- reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE)
- for row in reader:
- print row
- import csv
- with open('some.csv', 'wb') as f:
- writer = csv.writer(f)
- writer.writerows(someiterable)
Registering a new dialect :
- import csv
- csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
- with open('passwd', 'rb') as f:
- reader = csv.reader(f, 'unixpwd')
- import csv, sys
- filename = 'some.csv'
- with open(filename, 'rb') as f:
- reader = csv.reader(f)
- try:
- for row in reader:
- print row
- except csv.Error, e:
- sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
Supplement :
* Dialects and Formatting Parameters
沒有留言:
張貼留言