Python operation csv file

Source: Internet
Author: User

1. What is a CSV file

The so-called CSV (Comma separated Values) format is the most common import and export format for spreadsheets and Databas Es. CSV format was used-many years prior to attempts-describe the format in a standardized-in RFC 4180 .

2.csv File Disadvantages

The lack of a well-defined standard means, subtle differences often exist in the data produced and consumed by differe NT applications. These differences can make it annoying to process CSV files from multiple sources. Still, while the delimiters and quoting characters vary, the overall format was similar enough that it's possible to write A single module which can efficiently manipulate such data, hiding the details of reading and writing the data from the P Rogrammer.

3.python Module csv.py

the csv  module implements classes to read and write tabular data in CSV format. It allows programmers to say, "write this data in the format preferred by Excel," or "read data from this file which is G Enerated by Excel, "without knowing the precise details of the CSV format used by Excel. Programmers can also describe the CSV formats understood by other applications or define their own special-purpose CSV for Mats.

He csv module ' s reader and writer objects read and write sequences. Programmers can also read and write data in dictionary form using the DictReader and DictWriter classes

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

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.

Parameter explanation:

Delimiter: Setting separators

QuoteChar: Setting a reference character

Quoting: Quotation mark option with 4 different quotes options

Defined as four variables in a CSV module:

Quote_all all fields are quoted regardless of the type.

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

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

All content in the Quote_none 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).

Import csvdef Testreader (file): With open (file, ' R ') as Csvfile:spamreader = Csv.reader (CSVFile, delimiter= ', ') for row in S Pamreader:print (', '. Join (Row)) if __name__ = = ' __main__ ': csvfile = ' test.csv ' Testreader (csvfile)
writer (csvfile[, dialect= ' Excel ' [, Fmtparam])

Parameter table (slightly: With reader, see above)

def testwriter (file): With open (file, ' W ') as Csvfile:spamwriter = Csv.writer (CSVFile, delimiter= ', ', quotechar= ' | ', Quoting=csv. Quote_minimal) Spamwriter.writerow ([' Spam '] * 5 + [' Baked Beans ']) spamwriter.writerow ([' Spam ', ' lovely Spam ', ' Wonderful Spam '])
DictReader (f,FieldNames = None,Restkey = None,restval = none,dialect = ' Excel ',* args,* * Kwds )

Creates an object that operates like a regular reader, but maps the information in each row to a OrderedDict key given by the optional fieldnames parameter.

The parameter of the field name is a sequence. If the field name is omitted, the value in the first row of the file F is used as the field name. An ordered dictionary retains its original ordering regardless of how the field name is determined.

If the number of fields in a row exceeds the field name, the remaining data is placed in a list and stored with None the field name specified by Restkey (default). If the number of fields in a non-empty row is less than the field name, the missing value is filled in None .

def testdictreader (file): # Faculties, majors, grades, student categories, class, school number, name, credit score, update time, class rank, total number of participating classes with open (file, ' RB ') as CSVFile: Dictreader = csv. Dictreader (CSVFile) for row in Dictreader:print (". Join ([row[' faculties '], row[' professional '], row[' study number '), row[' name ']))
DictWriter (f,FieldNames,restval = ",extrasaction = ' raise ',dialect = ' Excel ',* args,* * Kwds )

Creates an object that runs like a normal writer, but maps the dictionary to the output line. The field name parameter is a sequence identity, writerow () in the order in which the keys are passed to the dictionary values Method is written to the file   ? F . The optional restval parameter specifies the value to write when the dictionary is missing the key of the field name . If passed to the writerow () the Dictionary of the method is contained in the The   key is not found in the field name, and the optional extrasaction parameter indicates the action to be performed. If set to ' raise ' default value, valueerror   a . If set to ' Ignore ' , the extra values in the dictionary will be ignored. Any other optional or keyword parameters are passed to the underlying  writer Instance.

Note that DictReader Unlike classes, thefieldnames parameter DictWriter is not optional. Because Python dict objects are not sorted, there is not enough information available to deduce the order in which the travel should be written to file F .

def testdictwriter (file): With open (file, ' W ') as Csvfile:fieldnames = [' faculties ', ' professional ', ' grade ', ' Student class ', ' class ', ' study number ']writer = CSV.D Ictwriter (CSVFile, Fieldnames=fieldnames) Writer.writeheader () Writer.writerow ({' Faculties ': ' Information academy ', ' Professional ': ' Computer science and Technology ', ' Grade ': ' Level 2011 ', ' Student class ': ' Undergraduate (undergraduate) 4 years ', ' class ': ' Computer 11 ', ' study number ': ' 201101245 ') writer.writerow (' Faculty ': ' Information academy ', ' Professional ': ' Computer science and Technology ', ' Grade ': ' 20 Level 11 ', ' Student category ': ' Undergraduate (undergraduate) 4 ', ' class ': ' Computer 11 ', ' School Number ': ' 201101275 '})

4. Sample Codecopy of CSV file
def copycsv (source, target): Csvtarget = open (target, ' w+ ') with open (source, ' R ') as Csvscource:reader = Csv.reader (Csvsco Urce, delimiter= ', ') for line in Reader:writer = Csv.writer (Csvtarget, delimiter= ', ', quotechar= ' | ', Quoting=csv. Quote_minimal) Writer.writerow (line) Csvtarget.close ()
5. Other methods (Numpy,pandas)
Import Numpymy_matrix = numpy.loadtxt (Open ("Num.csv", "RB"), delimiter= ",", skiprows=0) print (My_matrix)
Import Pandas as PD
Obj=pd.read_csv (' test.csv ') print Objprint type (obj) print obj.dtypes
Test.csv
Faculties, majors, grades, student categories, class, school number, name, credit score, update time, class ranking, number of participants in the class ranking information Institute, Computer science and technology, level 2011, Undergraduate (undergraduate) 4 years, computer 11,201101244, Luan, 86.72,2017/9/5 9:59,1,27 School of Information, computer science and technology, level 2011, Undergraduate (undergraduate) 4, computer 11,201101237, Liu, 86.05,2017/9/5 9:59,2,27 School of Information, computer science and technology, level 2011, Undergraduate (undergraduate) 4 years, Computer 11,201101233, Liu, 86.03,2017/9/5 9:59,3,27 School of Information, computer science and technology, level 2011, Undergraduate (undergraduate) 4 years, Computer 11,201101250, Lee, 85.43,2017/9/5 9:59,4,27 School of Information, computer science and technology, level 2011, Undergraduate (undergraduate) 4, computer 11,201101229, Zhang, 82.35,2017/9/5 9:59,5,27 School of Information, computer science and technology, level 2011, Undergraduate (undergraduate) 4 years, Computer 11,201101241, Han, 80.92,2017/9/5 9:59,6,27 School of Information, computer science and technology, level 2011, Undergraduate (undergraduate) 4 years, computer 11,201101232, ding, 80.66,2017/9/5 9:59,7,27 School of Information, computer science and technology, level 2011, Undergraduate (undergraduate) 4, computer 11,201101228, Zhang, 79.61,2017/9/5 9:59,8,27 School of Information, computer science and technology, level 2011, Undergraduate (undergraduate) 4 years, Computer 11,201101255, Meng, 79.55,2017/9/5 9:59,9,27
Num.csv
1,2,34,5,67,8,9
full code
# coding:utf-8import Csvdef testreader (file): With open (file, ' R ') as Csvfile:spamreader = Csv.reader (CSVFile, delimiter= ', ') for row in Spamreader:print (', '. Join (Row)) def testwriter (file): With open (file, ' W ') as Csvfile:spamwriter = Csv.writ ER (csvfile, delimiter= ', ', quotechar= ' | ', Quoting=csv. Quote_minimal) Spamwriter.writerow ([' Spam '] * 5 + [' Baked Beans ']) spamwriter.writerow ([' Spam ', ' lovely Spam ', ' Wonderful Spam ']) def copycsv (source, target): Csvtarget = open (target, ' w+ ') with open (source, ' R ') as Csvscource:reader = C Sv.reader (Csvscource, delimiter= ', ') for line in Reader:writer = Csv.writer (Csvtarget, delimiter= ', ', quotechar= ' | '), Quoting=csv. Quote_minimal) Writer.writerow (line) csvtarget.close () def testdictreader (file): # Faculties, majors, grades, student categories, class, school number, name, credit score, update time, Class ranking, participation in class ranking total number with open (file, ' RB ') as Csvfile:dictreader = csv. Dictreader (CSVFile) for row in Dictreader:print ('. Join ([row[' faculties '], row[' professional '], row[' study number '), row[' name ']) def Testdictwriter (file): With open (file, ' W ') as Csvfile:fieldnames = [' faculties ', ' professional ', ' grade ', ' Student class ', ' class ', ' study number ']writer = csv. Dictwriter (CSVFile, Fieldnames=fieldnames) Writer.writeheader () Writer.writerow ({' Faculties ': ' Information academy ', ' Professional ': ' Computer science and Technology ', ' grade ') : ' Level 2011 ', ' Student category ': ' Undergraduate (undergraduate) 4 years ', ' class ': ' Computer 11 ', ' study number ': ' 201101245 ') writer.writerow (' Faculty ': ' Information academy ', ' Professional ': ' Computer science and Technology ', ' Grade ': ' 2 Level 011 ', ' Student category ': ' Undergraduate (undergraduate) 4 ', ' class ': ' Computer 11 ', ' study number ': ' 201101275 ') def testpandas_csv (): Import pandas as Pdobj = Pd.read_csv (' Te St.csv ') print Objprint type (obj) print obj.dtypesdef testnumpy_csv (): Import Numpymy_matrix = numpy.loadtxt (Open (" Num.csv "," RB "), delimiter=", ", skiprows=0) print (My_matrix) if __name__ = = ' __main__ ': # csvfile = ' test.csv ' # Testreader ( CSVFile) # csvfile = ' test2.csv ' # testwriter (csvfile) # copycsv (' test.csv ', ' Testcopy.csv ') # testdictreader (' Test.csv ') # testdictwriter (' Test2.csv ') testnumpy_csv () # testpandas_csv ()

  

Python operation csv file

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.