Python STL CSV

Source: Internet
Author: User
Tags ord

Function: Read and write comma separated value files.

Python version: 2.3 and later versions

You can use the CSV module to process data exported from spreadsheets and databases and to write text files in fields and record formats, often called comma-separated values (comma-separated value, CSV) format, because common commas are used to separate fields in a record.

Read the file

You can use reader to create an object to read data from a CSV file. This reader can be used as an iterator to process the rows in the file sequentially, for example:

import csvwithopen("testdata.csv"'rt'as f:    = csv.reader(f)    forin reader:        print row

The first parameter of reader () is the source of the text line. In this example, this is a file, but it can also be any object that can be iterated (such as Stringio instances, lists, and so on). You can also specify additional optional parameters to control how the input data is parsed.

"Title 1","Title 2","Title 3"1,"a",08/18/072,"b",08/19/073,"c",08/20/07

When a file is read, each line of input data is parsed and converted to a list of strings.

['Title 1''Title 2''Title 3']['1''a''08/18/07']['2''b''08/19/07']['3''c''08/20/07'in0.1s]

This parser handles line breaks in the inline string, and for this reason, the row is not necessarily the same as an input line to the file.

Write a file

Writing a CSV file is as easy as reading a CSV file. You can use writer to create an object to write data, and then use Writerow to iterate through the lines of text to print.

ImportCsv with Open("Testout.csv",' WT ') asF:writer=Csv.writer (f) Writer.writerow (' Title 1 ',' Title 2 ',' Title 3 ') ) forIinch Range(3): Writer.writerow ((i+1,CHR(Ord(' A ')+i),' 08/%02d/07 ' %(I+1),                          )                         )Print Open("Testout.csv",' RT '). Read ()

The output here does not look exactly the same as the exported data used in the reader sample.

1231,a,08/01/072,b,08/02/073,c,08/03/07in0.1s]
Quotes

For writers, the default quotation marks behave differently, so the 2nd and 3rd columns in the preceding column are not quoted. To add quotation marks, you need to set the quoting parameter to a different quotation mark pattern.

= csv.writer(f, quoting=csv.QUOTE_NONNUMERIC)

Here, Quote_nonnumeric quotes around all columns that contain non-numeric content

ImportCsv with Open("Testout_quoted.csv",' WT ') asF:writer=Csv.writer (F, quoting=Csv. Quote_nonnumeric) Writer.writerow ((' Title 1 ',' Title 2 ',' Title 3 ') ) forIinch Range(3): Writer.writerow ((i+1,CHR(Ord(' A ')+i),' 08/%02d/07 ' %(I+1)) )Print Open("Testout_quoted.csv",' RT '). Read ()

Operation Result:

"Title 1","Title 2","Title 3"1,"a","08/01/07"2,"b","08/02/07"3,"c","08/03/07"in0.1s]

There are 4 different quotes options, defined as 4 constants in a CSV module.

    • Quote_all

All fields are quoted regardless of the type.

    • Quote_minimal

Enclose fields that contain special characters (so-called special characters, which can cause confusing characters for a parser configured with the same dialect and options). This is the default option

    • Quote_no_meric

Enclose all non-integer or floating-point fields in quotation marks. When used in a reader, an unquoted input field is converted to a floating-point number.

    • Quote_none

All content in the output is not quoted. When used in a reader, the quote characters are included in the field values (normally, they are processed as delimiters and stripped).

Dialect

A comma-separated value file does not have a well-defined standard, so the parser must be flexible. This flexibility means that a number of parameters can be used to control how the CSV parses or writes data. Instead of passing individual parameters into the reader and writer, you can group them together to form a dialect dialect object.

The dialect class can be registered by name so that the caller of the CSV Fumihiko block does not need to know the parameter settings in advance. Can be used List_ dialects. Gets the complete list of registered dialects.

import csvprint csv.list_dialects ()

This standard library consists of two dialects: Excel and Excel-tabs. Excel dialects are used to work with data in the default export format of Microsoft Excel, or you can handle OpenOffice or NeoOffice.

['excel-tab''excel'in0.1s]
Create a dialect

You can separate the segments without using commas, and the input file uses a vertical bar (|) as follows:

"Title 1"|"Title 2"|"Title 3"1|"first linesecond line"|08/18/07

You can register a new dialect with the appropriate delimiter.

import csvcsv.register_dialect('pipes', delimiter='|')with open('testdata.pipes', 'r') as f:    reader = csv.reader(f, dialect='pipes')    for row in reader:        print row

Using the "pipes" dialect, you can read the file just like a comma-delimited file.

['Title 1''Title 2''Title 3']['1''first line\nsecond line''08/18/07'in0.1s]

Additional parameters can be viewed with help.

Field name

In addition to working with data sequences, the CSV module includes classes that can be processed as a dictionary so that fields can be named. The Dictreader and Dictwriter classes convert rows to dictionaries instead of lists. The dictionary key can be passed in, or it can be deduced from the first line of input (if the row contains a header).

import csvwithopen("testdata.csv"'rt'as f:    = csv.DictReader(f)    forin reader:        print row

Dictionary-based readers and writers are implemented as wrappers for sequence-based classes that use the same methods and parameters. The only difference in the reader API is that the row will be returned as a dictionary instead of as a list or tuple.

{'Title 1''1''Title 3''08/18/07''Title 2''a'}{'Title 1''2''Title 3''08/19/07''Title 2''b'}{'Title 1''3''Title 3''08/20/07''Title 2''c'in0.1s]

You must provide a list of field names for Dictwriter, so that it knows how to determine the order of the columns in the output.

ImportCsv with Open("Testout.csv",' WT ') asF:fieldnames=(' Title 1 ',' Title 2 ',' Title 3 ') headers= Dict((N,n) forNinchFieldNames) writer=Csv. Dictwriter (F, FieldNames=FieldNames) Writer.writerow (headers) forIinch Range(3): Writer.writerow ({' Title 1 ': I+1,' Title 2 ':CHR(Ord(' A ')+i),' Title 3 ':' 08/%02d/07 ' %(I+1),                          })Print Open("Testout.csv",' RT '). Read ()

Field names are not automatically written to the file, so you need to write them explicitly before writing other data.

1231,a,08/01/072,b,08/02/073,c,08/03/07in0.1s]

Python STL CSV

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.