Introduction and use of the Python CSV module tutorial

Source: Internet
Author: User
Tags in python

python CSV module introduction


CSV is a very common and very simple way to store large amounts of data, and there are CSV modules in Python that deal with this part of the content. Here is a brief introduction to the simplest reading and writing:

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

Parameters:

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

Dialect is a delimited style, and the default is Excel style, which is comma-delimited. Excel-tab style is also supported, that is, tab-delimited.

Use examples:

Import CSV

Reader = Csv.reader (file (' my.csv ', ' RB '))
For line in reader:
Print Line

Writer

writer = csv.writer (open (TargetFile, "WB"), Quoting=csv. Quote_all)

The first parameter represents the file object to write, and the specific write can be implemented through the Writerow and Writerows functions:

Writer.writerow (["121", "121"])

#传入2纬list
Writer.writerows ([[["121", "121"],["345", "" 345 "]])


use of the Python CSV module

Functions in a CSV module

Reader (CSVFile, dialect= ' Excel ', **fmtparams)

Parameter description:

CSVFile, must be an object that supports iteration (iterator), can be a file object or List object, if the file is
Like, you need to add the "B" flag parameter when opening.


Dialect, coding style, default to Excel style, that is, separated by commas (,), dialect ways also support customization, by invoking the Register_dialect method to register, as mentioned below.


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


Import CSV
With open (' test.csv ', ' RB ') as MyFile:
Lines=csv.reader (MyFile)
For line in lines:
Print Line


' Test.csv ' is the filename, and R in ' RB ' means ' read ' mode, because it is a file object, so add ' B '. Open () returns a File object

Myfile,reader (MyFile) only passes in the first argument, and the other two parameters take the default value, which is read in Excel style. Reader () returns a

The reader object Lines,lines is a list that returns a string when the method Lines.next () is invoked. The effect of the above program is to convert the CSV

The text in the file is printed in rows, and the elements of each row are separated by a comma separator ', '.


In my test.csv file, the data stored in the diagram is as follows:



Program output:

[' 1 ', ' 2 ']
[' 3 ', ' a ']
[' 4 ', ' B ']


Add: The reader object also provides some methods: Line_num, dialect, next ()


Writer (csvfile, dialect= ' Excel ', **fmtparams)

The meaning of the parameter ditto, here do not repeat, directly on the routine:

With open (' T.csv ', ' WB ') as MyFile:
Mywriter=csv.writer (MyFile)
Mywriter.writerow ([7, ' G '])
Mywriter.writerow ([8, ' H '])
mylist=[[1,2,3],[4,5,6]]
Mywriter.writerows (MyList)


' W ' means write mode.

The open () function first opens the file named ' T.csv ' under the current path, and if it does not exist, creates it and returns the MyFile file object.

Csv.writer (MyFile) Returns the writer object mywriter.

The Writerow () method is written one line, and the Writerows method writes multiple lines at a time.

Note: If the file ' T.csv ' exists beforehand, calling the writer function empties the text in the original file and then executes the Writerow/writerows method.


Add: In addition to Writerow, Writerows,writer objects also provide a number of other methods: Writeheader, dialect



Register_dialect (name, [dialect,]**fmtparams)

This function is dialect from the definition.

Parameter description:

Name, your custom dialect, for example, the default is ' Excel ', which you can define as ' mydialect '


[dialect,]**fmtparams,dialect format parameters, with delimiter (delimiter, default is comma), QuoteChar,

Quoting and so on, you can refer to dialects and formatting Parameters


Csv.register_dialect (' Mydialect ', delimiter= ' | ', Quoting=csv. Quote_all)


The above line program customizes a dialect named Mydialect, and the parameters only set the delimiter and quoting two, and the others still use

Default value, where ' | ' As the separator character. Then we can use ' mydialect ' just like ' Excel '. Let's look at the effect:


Store the following data in my test.csv:


Print in ' Mydialect ' style:

With open (' test.csv ', ' RB ') as MyFile:
Lines=csv.reader (MyFile, ' mydialect ')
Print Lines.line_num
For line in lines:
Print Line


Output:

[' 1,2 ', ' 3 ']
[' 4,5 ', ' 6 ']


As you can see, now is the ' | ' For delimiters, 1 and 2 synthesize a string (because the delimiter between 1 and 2 is a comma, and the mydialect style is delimited

Symbol is ' | ' ), 3 a single string.

For the writer () function, you can also pass in the mydialect as an argument, not to repeat here.


Unregister_dialect (name)

This function is used to unregister custom dialect


In addition, the CSV module also provides functions such as Get_dialect (name), List_dialects (), Field_size_limit ([New_limit]), which are compared

Simple, you can try it yourself. For example, the List_dialects () function lists all the dialect in the current CSV module:

Print csv.list_dialects ()


Output:

[' Excel-tab ', ' Excel ', ' Mydialect ']

' Mydialect ' is a custom, ' Excel-tab ', ' Excel ' is a dialect, where ' Excel-tab ' is about the same as ' Excel ',

It's just a tab as a separator.


The CSV module also defines

Some classes: Dictreader, Dictwriter, dialect, Dictreader and Dictwriter are similar to reader and writer.

Some constants: Quote_all, Quote_minimal,. Quote_nonnumeric, these constants can be used as dialects and formatting parameters values.

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.