Python CSV module Use instance _python

Source: Internet
Author: User

Give a few examples of how Python's CSV modules are used, including reader, writer, Dictreader, Dictwriter.register_dialect

have been very fond of Python csv module, easy to use, often used in the project, now give a few examples to illustrate.

Copy Code code as follows:

Reader (csvfile[, dialect= ' Excel '] [, Fmtparam])

Parameter table:

CSVFile
needs to be an object that supports iteration (iterator), and the return value of each call to the next method is a string (string), the usual file object, or the list object is applicable, and if it is a file object, the "B" flag argument is required.

Dialect
the encoding style, which defaults to Excel, is comma (,) delimited, and the CSV module supports the Excel-tab style, which is tab-delimited. 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 encoded styles that have been registered.

Fmtparam
formatting parameters that overwrite the encoding style specified by the previous dialect object.

Example:

Copy Code code 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: Same reader, see above)

Example:

Copy Code code 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, is read CSV, but will generate a dictionary (dict) type of return, rather than the iteration type.

Dictwriter

My main point is dictwriter, why I like to use Dictwriter, because the ordinary writer you need to manually build the list, especially through the form submission, and I have been on the Zope platform for the development, and Zope support an advanced form data model , that is, the form can be defined by adding the appropriate flag to enable the submission of the form data automatically generated a record (records) type, that is, to generate a list of each data is a dictionary. In this way, I can easily pass the form data directly to Dictwriter and generate a CSV, of course, this is the premise that you can guarantee the correctness of the data. OK, let me briefly explain the advanced form data type of Zope.

Example:

Copy Code code as follows:

<form action= ' test_form_action ' method=post>
<input type= "text" name= "rows. Column1:records "value=" 0 "/>
<input type= "text" name= "rows. Column2:records "value=" 1 "/>
<input type= "text" name= "rows. Column3:records "value=" 2 "/>
<input type= "text" name= "rows. Column4:records "value=" 3 "/>
<br/>
<input type= "text" name= "rows. Column1:records "value=" 0 "/>
<input type= "text" name= "rows. Column2:records "value=" 1 "/>
<input type= "text" name= "rows. Column3:records "value=" 2 "/>
<input type= "text" name= "rows. Column4:records "value=" 3 "/>
<br/>
<input type= "text" name= "rows. Column1:records "value=" 0 "/>
<input type= "text" name= "rows. Column2:records "value=" 1 "/>
<input type= "text" name= "rows. Column3:records "value=" 2 "/>
<input type= "text" name= "rows. Column4:records "value=" 3 "/>
<br/>
<input type= "text" name= "rows. Column1:records "value=" 0 "/>
<input type= "text" name= "rows. Column2:records "value=" 1 "/>
<input type= "text" name= "rows. Column3:records "value=" 2 "/>
<input type= "text" name= "rows. Column4:records "value=" 3 "/>
<br/>
<input type= "text" name= "rows. Column1:records "value=" 0 "/>
<input type= "text" name= "rows. Column2:records "value=" 1 "/>
<input type= "text" name= "rows. Column3:records "value=" 2 "/>
<input type= "text" name= "rows. Column4:records "value=" 3 "/>
<br/>
<input type= "Submit" value= "Submit CSV"/>
</form>

The results of the form submission are:
Copy Code code 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 call the Dictwriter.writerows method directly to handle:
Copy Code code as follows:

Import CSV

FieldNames = [' Column1 ', ' Column2 ', ' Column3 ', ' Column4 ']
Dict_writer = csv. Dictwriter (' your.csv ', ' WB '), Fieldnames=fieldnames)
Dict_writer.writerow (fieldnames) # CSV first line needs to join
Dict_writer.writerows (rows) # Rows is the data submitted by the form

* Note: The CSV file write here requires the support of external method, because the problem with the permissions sandbox in Zope is that it cannot directly manipulate the CSV module to read and write to the file system.



This is not very convenient to use, here to generate the form above the dtml code:
Copy Code code as follows:

<form action= ' Test_form ' method=post>
<dtml-in "Range (5)" >
<dtml-in "Range (4)" >
<input type= "text" name= "rows. Column&dtml-sequence-number;:records "value=" &dtml-sequence-item; "/>
</dtml-in>
<br/>
</dtml-in>
<input type= "Submit" value= "Submit CSV"/>
</form>

You can overwrite the form's build based on your own needs.

Reference documents:
Http://docs.python.org/lib/module-csv.html
http://www.python.org/dev/peps/pep-0305/

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.