6-1 How to read and write csv data

 

>>> from urllib import urlretrieve
 >>> urlretrieve( ' http://table.finance.yahoo.com/table.csv?s=000001.sz ' ,r ' C:\video\python efficient practice tips\6 Data encoding and processing related topics\pingan.csv ' ,'pingan.csv')

The first parameter of the Urlretrieve module is the url, and the second parameter is the file to save the opened file.

Use the reader() and writer() functions of the CSV interface to read and write CSV files

Because this URL can't be opened, so download a CSV file from the Internet and operate directly

1. Open and read a csv file

>>> help(csv.reader)
Help on built-in function reader in module _csv:

reader(...)
    csv_reader = reader(iterable [, dialect='excel']
                            [optional keyword args])
        for row in csv_reader:
            process(row)
    
    The "iterable" argument can be any object that returns a line
    of input for each iteration, such as a file object or a list.  The
    optional "dialect" parameter is discussed below.  The function
    also accepts optional keyword arguments which override settings
    provided by the dialect.
    
    The returned object is an iterator.  Each iteration returns a row
of the CSV file (which can span multiple input lines).
help(csv.reader)

The argument is a file object to open the file, and the return is an iterator.

>>> import csv
>>> 
>>> rf = open(r " C:\video\python efficient practice skills notes\6 topics related to data encoding and processing\bank-data.csv " , ' rb ' )      #Note that opening csv files requires binary opening 
> >> reader = csv.reader(rf)
 >>> reader.next()        # csv.reader() returns an iterator, you need to use the next() method 
[ ' id ' , ' age ' , ' sex ' , ' region ' , ' income ' , ' married ' , 'children', 'car', 'save_act', 'current_act', 'mortgage', 'pep']
>>> reader.next()
['ID12101', '48', 'FEMALE', 'INNER_CITY', '17546.0', 'NO', '1', 'NO', 'NO', 'NO', 'NO', 'YES']

See how reader() returns an iterable object

>>> help(reader)
Help on reader object:

class reader(__builtin__.object)
 |  CSV reader
 |  
 |  Reader objects are responsible for reading and parsing tabular data
 |  in CSV format.
 |  
 |  Methods defined here:
 |  
 |  __iter__(...)
 |      x.__iter__() <==> iter(x)
 |  
 |  next(...)
 |      x.next() -> the next value, or raise StopIteration
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  dialect
 |  
 |  line_num
help(reader)

2. Open and write a file

>>> writer = csv.writer(wf)
>>> help(csv.writer)
Help on built-in function writer in module _csv:

writer(...)
    csv_writer = csv.writer(fileobj [, dialect='excel']
                                [optional keyword args])
        for row in sequence:
            csv_writer.writerow(row)
    
        [or]
    
        csv_writer = csv.writer(fileobj [, dialect='excel']
                                [optional keyword args])
        csv_writer.writerows(rows)
    
    The "fileobj" argument can be any object that supports the file API.
help(csv.writer)

Like reader() , the parameter is an object to open the file, the return value is an iterator, and the way to open the file needs to use binary mode

 

>>> wf = open(r ' C:\video\python efficient practice skills notes\6Data encoding and processing related topics\bank-databak.csv ' , ' wb ' )
 >>> writer = csv.writer(wf)
 >>> writer.writerow([ ' id ' , ' age ' , ' sex ' , ' region ' , ' income ' , ' married ' , ' children ' , ' car ' , 'save_act', 'current_act ' , ' mortgage ' , ' pep ' ])
 >>> writer.writerow(reader.next())
 >>> writer.writerow(reader.next())
 >>> wf.flush()          # and c language The flush() function is the same, and the content of the file buffer is output to the file in time.

The main way to view the iterator returned by writer() is writerow() to write rows

>>> help(writer)
Help on writer object:

class writer(__builtin__.object)
 |  CSV writer
 |  
 |  Writer objects are responsible for generating tabular data
 |  in CSV format from sequence input.
 |  
 |  Methods defined here:
 |  
 |  writerow(...)
 |      writerow(sequence)
 |      
 |      Construct and write a CSV record from a sequence of fields.  Non-string
 |      elements will be converted to string.
 |  
 |  writerows(...)
 |      writerows(sequence of sequences)
 |      
 |      Construct and write a series of sequences to a csv file.  Non-string
 |      elements will be converted to string.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  dialect
help(writer)

Bank-data.csv is an income situation. Now write a script to save the files whose age is between 30-40 and whose income is greater than 10,000 to a bank-databak.csv file. Now write the script as follows:

# -*- coding: cp936 -*-
import csv
with open(r ' C:\video\python efficient practice skills notes\6Data encoding and processing related topics\bank-data.csv ' , ' rb ' ) as rf:
    reader = csv.reader(rf)
    with open(r ' C:\video\python efficient practical skills notes\6Data encoding and processing related topics\bank-databak.csv ' , ' wb ' ) as wf:
        writer = csv.writer(wf)
        header = reader.next()     #Read the header of the file 
        writer.writerow(header) #Write    the header of the file to the file

        for row in reader:         #Iteratively execute the judgment, the first field in the file represents age and the fourth field represents income #The 
characters read in the file, so the characters should be converted into int or float when comparing. 
# Judgment in python The range of numbers can be expressed in mathematics, which is different from the C language. 
            if (30< int(row[1])<40 ​​and float(row[4])>10000.0 ):
                writer.writerow(row) #Write    appropriate data to a new csv file 
print ( " end " )

Result :

Ps:

Note that the size comparison of the string date comparison can also use the following red box method,

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324862906&siteId=291194637