Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download file as string in python

I want to download a file to python as a string. I have tried the following, but it doesn't seem to work. What am I doing wrong, or what else might I do?

from urllib import request

webFile = request.urlopen(url).read()
print(webFile)
like image 779
Jakob Weisblat Avatar asked Apr 15 '13 21:04

Jakob Weisblat


1 Answers

The following example works.

from urllib.request import urlopen

url = 'http://winterolympicsmedals.com/medals.csv'
output = urlopen(url).read()
print(output.decode('utf-8'))

Alternatively, you could use requests which provides a more human readable syntax. Keep in mind that requests requires that you install additional dependencies, which may increase the complexity of deploying the application, depending on your production enviornment.

import requests

url = 'http://winterolympicsmedals.com/medals.csv'
output = requests.get(url).text
print(output)
like image 108
eandersson Avatar answered Sep 23 '22 04:09

eandersson