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)