2
from pip._vendor import requests

import csv

url = 'https://docs.google.com/spreadsheets/abcd'

dataReader = csv.reader(open(url), delimiter=',', quotechar='"')

exampleData = list(dataReader)

exampleData

3 Answers 3

1

Use Python Requests.

import requests
r = requests.get(url)
lines = r.text.splitlines()

We use splitlines to turn the text into an iterable like a file handle. You should probably wrap it up in a try, catch block in case of errors.

Sign up to request clarification or add additional context in comments.

2 Comments

cannot import requests
It's a library, you need to install it. Do a search online for python pip it's the package manager for python. If you couldn't be bothered run... > pip install requests
1

You need to use something like urllib2 to retrieve the file. for example:

import urllib2
import csv
csvfile = urllib2.urlopen('https://docs.google.com/spreadsheets/abcd')
dataReader = csv.reader(csvfile,delimiter=',', quotechar='"')
do_stuff(dataReader)

2 Comments

cannot import urllib2
It is a lib that you download.
0

You can import urllib.request and then simply call data_stream = urllib.request.urlopen(url) to get a buffer of the file. You can then save the csv data as data = str(data_stream.read(), which may be a bit unclean depending on your source or encoded, so you may need to do some manipulation, but if not then you can just throw it into csv.reader(data, delimiter=',')

An example requiring translating from byte format that may work for you:

data = urllib.request.urlopen(url)
data_csv = str(data.read())
# split out b' flag from string, then also split at newlines up to the last one
dataReader = csv.reader(data_csv.split("b\'",1)[1].split("\\n")[:-1], delimiter=",")
headers = reader.__next__()
exampleData = list(dataReader)

1 Comment

No problem glad I could help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.