Python reads text

Source: Internet
Author: User

Converting text into a numpy array, doing machine learning or any other task, is essential for text processing skills. The python implementation enables very thin and powerful text processing capabilities:

Suppose the file traindata.csv has data 1000 rows, 3 column features, and the fourth column (the last column) is the class label

1. Basic methods:

def File2matrix ():     = []    = []    = open ('./traindata.csv','RB ' )  
Fr.readline () for in fr.readlines (): #读取每一行
CurLine = Line.strip (). Split (' \ t ')
Linearr = []
For I in range (3):
Linearr.append (float (curline[i)) # Read each property
Datamat.append (Linearr)
Labelmat.append (float (curline[3]))
Return Datamat,labelmat

2. Using the CSV module

Import CSV def File2matrix ():      = Open ('./traindata','rb')     =  Csv.reader (FR) lines.next ()  // Ignore first line      for in   lines:           .... 

3. Using the Pandas module

ImportPandas as PDdefFile2matrix (): Fr= Open ('./traindata.csv','RB') DF= Pd.read_csv (fr,header=0) Datamat= df[['Feature1','Feature2','Feature3']] Labelmat= df['label']    returnDatamat,labelmat

Obviously, if the skilled palmprint pandas will be very simple, so easy.

The end of the paper is shallow, I know this matter to preach ....

Just do it!

Python reads text

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.