Python provides four implementation methods for downloading network text data to local memory.

Source: Internet
Author: User

Python provides four implementation methods for downloading network text data to local memory.

This example describes how to download network text data to the local memory using Python. We will share this with you for your reference. The details are as follows:

Import urllib. requestimport requestsfrom io import StringIOimport numpy as npimport pandas as pd ''' to download network files, and import the CSV file as the numpy matrix ''' # Network Data File url = "http://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data" # method 1 #==================== ========================================================== = # download an object # r = urllib. request. urlopen (url) # import the CSV file as the numpy matrix # dataset = np. loadtxt (r, delimi Ter = ",") # method 2 #================================================= ==============================## download file # r = requests. get (url) # import the CSV file as the numpy matrix # dataset = np. loadtxt (StringIO (r. text), delimiter = ",") # StringIO is used here !!!!!! # Method 3 #================================================= ==============================## use genfromtxt to directly download network files, export the CSV file as the numpy matrix. Great !!!!!!!! # Dataset = np. genfromtxt (url, delimiter = ",") # Method 4 #================================================= ==============================## use pandas. read_csv directly downloads the network file and imports the CSV file as pandas. dataFrame. # Dataset = pd. read_csv ('HTTP: // www-bcf.usc.edu /~ Gareth/ISL/Advertising.csv ', index_col = 0) dataset = pd. read_csv (url) #===================================================== ====================## separate the data from the target attributesX = dataset [:, 0: 7] y = dataset [:, 8] print (X) # print (y)

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.