Use of the Python CSV module

Source: Internet
Author: User

1. Introduction to CSV

CSV (Comma separated Values), which is a comma-separated value (also called a character-delimited value, because the delimiter can be not a comma), is a common text

Format for storing tabular data, including numbers or characters. Many programs in the processing of data will encounter the CSV format of the file, its use is more than

The more extensive (the data provided on some topics on Kaggle is CSV format), CSV is widely used, but there is no common standard, so in the process of CSV

Format, and fortunately Python has a CSV module built into it. The following is a brief introduction to some of the most commonly used functions in the CSV module.

For more information, please refer to: https://docs.python.org/2/library/csv.html#module-csv

2. Functions in the CSV module
    • Reader (CSVFile, dialect= ' Excel ', **fmtparams)
parameter Description:
CSVFile, must be an object that supports iterations (Iterator), which can be a file object or a list object, or a file object that requires a "B" flag parameter when opened.
dialect, coding style, the default is the style of Excel, that is, separated by commas (,), dialect mode also supports customization, by calling the Register_dialect method to register, the following will be mentioned.
Fmtparam, a format parameter used to override the encoding style specified by the previous dialect object.

[Python]View PlainCopy
    1. Import CSV
    2. With open (' test.csv ',' RB ') as MyFile:
    3. Lines=csv.reader (MyFile)
    4. For line in lines:
    5. Print Line

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

Myfile,reader (MyFile) only passes in the first parameter, and the other two parameters are read in the Excel style by default values. Reader () returns a

Reader Object Lines,lines is a list that returns a string when its method Lines.next () is called. The effect of the above program is to add the CSV

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

In my test.csv file, the stored data

Program output:

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

supplement:Reader object also provides some methods: Line_num, dialect, next ()

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

The meaning of the parameter is the same as above, do not repeat here, directly on the routine:

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

' W ' indicates write mode.

The open () function first opens the file with the name ' T.csv ' under the current path and if the file does not exist, it is created, returning the MyFile file object.

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

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

Note: If the file ' T.csv ' is pre-existing, calling the writer function empties the text in the original file before executing the Writerow/writerows method.

added: In addition to Writerow, Writerows,writer objects also provide some other methods: Writeheader, dialect

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

This function is used to customize the dialect.

Parameter description:

Name, your custom dialect, such as the default is ' Excel ', you can define it as ' mydialect '

[dialect,]**fmtparams,dialect format parameter, have delimiter (delimiter, default is comma), QuoteChar,

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

[Python]View PlainCopy
    1. Csv.register_dialect (' Mydialect ', delimiter=' | ', Quoting=csv. Quote_all)

The above line of the program has customized a dialect named Mydialect, the parameters only set the delimiter and quoting two, the other is still used

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

The following data is stored in my test.csv:

Print in ' Mydialect ' style:

[Python]View PlainCopy
    1. With open (' test.csv ',' RB ') as MyFile:
    2. Lines=csv.reader (MyFile,' Mydialect ')
    3. Print Lines.line_num
    4. For line in lines:
    5. Print Line


Output:

[' Up ', ' 3 ']
[' 4,5 ', ' 6 ']

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

The symbol is ' | ' ), 3 a single string.

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

    • Unregister_dialect (name)

This function is used to unregister custom dialect

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

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

[Python]View PlainCopy
    1. Print csv.list_dialects ()


Output:

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

' Mydialect ' is a custom, ' Excel-tab ', ' Excel ' is a dialect, where ' Excel-tab ' is similar to ' Excel ',

It's just a tab-delimited character.

The CSV module also defines

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

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

Use of the Python CSV module

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.