Python loads CSV files in two ways __python

Source: Internet
Author: User

This article mainly explains the Python loading CSV file in two ways, if you know how to deal with it do not have to look down.

Here is a brief introduction to the following.

The dataset in the instance is the Train.csv file of the kaggle digit recognizer, the data format is very special and very common, the screenshot is as follows:

Each characteristic column of each row of data in a CSV file is separated by a comma "." In addition to saving values in the CSV file, there are some property values, such as the label label for the first row and the first column. Therefore, the processing is required to be handled accordingly.

Here are two ways to load this CSV file : Using 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.

Load file code:

Import CSV  
def loadCSVfile1 ():
    list_file = []
    with open (' train.csv ', ' RB ') as Csv_file:  
        all_lines= Cvs.reader (csv_file) for  
        one_line in All_lines:  
            list_file.append (one_line)  
    List_file.remove (list_file [0])
    Arr_file = Array (list_file)
    label = arr_file[:, 0]
    data = arr_file[:, 1:] return
    data, label  

Load the CSV kernel part or

where R in ' RB ' means ' read ' mode, because it is a file object, 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 All_lines,all_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 ', '. method One: Use Python's numpy library

With the Numpy,python for data processing can be said to be more powerful, speed also greatly improved. The first thing to do with NumPy is to load the NumPy library before the following code:

Import NumPy as NP

For the above CSV file, the following program is used to read the data section:

Def loadCSVfile2 ():
    tmp = Np.loadtxt ("Train.csv", Dtype=np.str, delimiter= ",")
    data = Tmp[1:,1:].astype ( Np.float) #加载数据部分
    label = Tmp[1:,0].astype (np.float) #加载类别标签部分 return
    data, label #返回array类型的数据

Three lines of code, very simple. The final result is the array type, type float64

The code is simple, just for reference.

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.