rsc05
rsc05

Reputation: 3820

Python: how to import csv file from github into text file

I have the following webpage on GitHub whereby I would like to import its content into python particularly jupyter notebook

import pandas as pd
url = 'https://github.com/stedy/Machine-Learning-with-R-datasets/blob/master/groceries.csv'
file1=open(url,'r')
UnOrgan=file1.read()

But it did not work

SError: [Errno 22] Invalid argument:

Can someone help me with this?

Upvotes: 2

Views: 2904

Answers (2)

Giovani
Giovani

Reputation: 175

Enter the webpage that you are trying to import and click Raw as shown below. Click Raw

Copy the address from your browser: "https://raw.githubusercontent.com/stedy/Machine-Learning-with-R-datasets/master/groceries.csv"

#Use the address here:
url = 'https://raw.githubusercontent.com/stedy/Machine-Learning-with-R-datasets/master/groceries.csv'
#Name the resulting dataframe
dataDF = pd.read_csv(url)

Upvotes: 2

Sam
Sam

Reputation: 636

You can't pass a URL to open(). Try using the requests library:

import requests

url = 'https://github.com/stedy/Machine-Learning-with-R-datasets/blob/master/groceries.csv'
response = requests.get(url)
print(response.text)

Upvotes: 2

Related Questions