Last active
December 19, 2017 00:29
-
-
Save BlogBlocks/9a2bd33b0263df26f22a8537e1abdbb5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Learn to download and save a CSV file | |
| open it and read the data | |
| """ | |
| import sys | |
| from itertools import islice | |
| import urllib2 | |
| # CO2.csv source | |
| url = 'http://pchart.sourceforge.net/Resources/CO2.csv' | |
| # Open the URL | |
| f = urllib2.urlopen(url) | |
| # Read the URL as data | |
| data = f.read() | |
| # Open a file to write the data to | |
| # Notice mode is open as wb " binary This also works to save images | |
| with open("co2.csv", "wb") as code: | |
| # With the file is open write the data to it. | |
| code.write(data) | |
| filename = "co2.csv" | |
| # Open the filename you just downloaded | |
| f = open(filename) | |
| ## Read the first line | |
| line = f.readline() | |
| with open(filename) as myfile: | |
| head = list(islice(myfile,1)) | |
| print "HEAD :",head,"\n\n" | |
| # If the file is not empty keep reading line one at a time | |
| ## till the file is empty | |
| x = 0 | |
| while line : | |
| while x <10: | |
| line = f.readline() | |
| x = x +1 | |
| print line | |
| f.close() | |
| sys.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment