Improvements to syntax and comments

Improve code by using context managers to handle closing of files.

Also, replace the flame-war indexing comment for one with some explanations to why things are as they are.
This commit is contained in:
Thales Mello 2018-03-08 18:03:32 -03:00 committed by GitHub
parent 35cfeef957
commit 3743c596d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -38,18 +38,16 @@ r.text # raw page source
print(r.text) # prettily formatted
# save the page source in a file:
os.getcwd() # check what's the working directory
f = open("learnxinyminutes.html", "wb")
f.write(r.text.encode("UTF-8"))
f.close()
with open("learnxinyminutes.html", "wb") as f:
f.write(r.text.encode("UTF-8"))
# downloading a csv
fp = "https://raw.githubusercontent.com/adambard/learnxinyminutes-docs/master/"
fn = "pets.csv"
r = requests.get(fp + fn)
print(r.text)
f = open(fn, "wb")
f.write(r.text.encode("UTF-8"))
f.close()
with open(fn, "wb") as f:
f.write(r.text.encode("UTF-8"))
""" for more on the requests module, including APIs, see
http://docs.python-requests.org/en/latest/user/quickstart/
@ -71,8 +69,8 @@ pets
# 1 vesuvius 6 23 fish
# 2 rex 5 34 dog
""" R users: note that Python, like most normal programming languages, starts
indexing from 0. R is the unusual one for starting from 1.
""" R users: note that Python, like most C-influenced programming languages, starts
indexing from 0. R starts indexing at 1 due to Fortran influnce.
"""
# two different ways to print out a column