Here are a few examples of how Python's CSV module is used, including, reader, writer, Dictreader, Dictwriter.register_dialect
Have always loved Python's CSV module, easy to use, often used in projects, now give a few examples to illustrate.
The code is as follows:
Reader (csvfile[, dialect= ' Excel ' [, Fmtparam])
Parameter table:
CSVFile
You need to be an object that supports iterations (Iterator), and each time the return value of the next method is called a string (string), the usual file object, or the list object is applicable, and if it is a file object, opening is required to add the "B" flag parameter.
Dialect
Encoding style, the default is Excel, that is, comma (,) separated, and the CSV module also supports Excel-tab style, that is, tab (tab) separation. Other ways need to be defined, and then you can call the Register_dialect method to register, and the List_dialects method to query the list of all coded styles that have been registered.
Fmtparam
A format parameter that overrides the encoding style specified by the previous dialect object.
Example:
The code is as follows:
Import CSV
Reader = Csv.reader (file (' your.csv ', ' RB '))
For line in reader:
Print Line
Writer (csvfile[, dialect= ' Excel ' [, Fmtparam])
Parameter table (slightly: With reader, see above)
Example:
The code is as follows:
Import CSV
writer = csv.writer (file (' Your.csv ', ' WB '))
Writer.writerow ([' Column1 ', ' Column2 ', ' Column3 '])
lines = [Range (3) for I in range (5)]
For line in lines:
Writer.writerow (line)
Dictreader
Similar to reader, it is used to read CSV, but generates a dictionary (dict) type of return, not an iteration type.
Dictwriter
My main point is dictwriter, why I like to use Dictwriter, because ordinary writer you need to manually build the list, especially through the form submission, and I have been developed on the Zope platform, and Zope supports an advanced form data model , that is, you can define the form by adding the appropriate flag to make the submitted form data automatically generate a record (records) type, that is, to generate a list of each data is a dictionary. In this way, I can easily direct the form data to Dictwriter and generate CSV, of course, this is in the premise that you can guarantee the correctness of the data. OK, let me briefly explain the advanced form data type of Zope.
Example:
The code is as follows:
The result of the form submission is:
The code is as follows:
rows = [{' Column1 ': ' 0 ', ' Column2 ': ' 1 ', ' Column3 ': ' 2 ', ' Column4 ': ' 3 '},
{' Column1 ': ' 0 ', ' Column2 ': ' 1 ', ' Column3 ': ' 2 ', ' Column4 ': ' 3 '},
{' Column1 ': ' 0 ', ' Column2 ': ' 1 ', ' Column3 ': ' 2 ', ' Column4 ': ' 3 '},
{' Column1 ': ' 0 ', ' Column2 ': ' 1 ', ' Column3 ': ' 2 ', ' Column4 ': ' 3 '},
{' Column1 ': ' 0 ', ' Column2 ': ' 1 ', ' Column3 ': ' 2 ', ' Column4 ': ' 3 '}]
This allows you to directly invoke the Dictwriter.writerows method to handle the following:
The code is as follows:
Import CSV
FieldNames = [' Column1 ', ' Column2 ', ' Column3 ', ' Column4 ']
Dict_writer = csv. Dictwriter (File (' Your.csv ', ' WB '), Fieldnames=fieldnames)
Dict_writer.writerow (fieldnames) # CSV first line needs to join itself
Dict_writer.writerows (rows) # Rows is the data submitted by the form
* Note: The CSV file written here requires support for external method because the problem with the permissions sandbox in Zope is that the CSV module cannot be directly manipulated to read and write to the file system.
This is not very convenient to use, here gives the dtml code to generate the above form:
The code is as follows:
You can override this form's build to your own needs.
Reference documents:
Http://docs.python.org/lib/module-csv.html
http://www.python.org/dev/peps/pep-0305/