<<Learn Python the Hard Way>> Notes

Categories: Python; Tagged with: ; @ May 14th, 2014 21:04

by Zed A. Shaw

Stop reading my book,  I don’t write it for you. I wrote it for people who don;t already know everything.

Empty before you fill. you will have a hard time learning from someone with more knowledge if you already know everything;

Go learn Lisp, I hear people who know everything really like Lisp.

A programmer may try to get you to install Python 3 and learn that, Say, “When all the Python code on your computer is Python 3, then Ii’ll try to learn it”

What I discovered after this journey of learning ( about 20 programming languages) is that it’s not the languages that matter but what you do with them … which programming language you learn and use doesn’t matter. Do not get sucked into the religion surrounding programming languages, as that will only blind you to their true purpose of being your tool for doing interesting things.

Programming as a profession is only moderately interesting. It can be a good job, but you could make about the same money and be happier running a fast food joint.  You’re much better off using code as your secret weapon in another profession.

Of course, all this advice is pointless.

Finally, I’ll say that learning to create software changes you and makes you different – not better or worse, just different. you may find that people treat you harshly because you can create software, maybe using words like  “nerd”. maybe you’ll find that because you can dissect their logic, they hate arguing with you . you may even find that simply knowing how a computer working makes you annoying and weird to them.  to this, I have just one piece of advice: they can go to hell.  the world needs more weird people who know how things work and who love to figure it all out. when they treat you like this, just remember that this is your journey, not theirs. Being different is not a crime, and people who tell you it is are just jealous that you’ve picked up a skill they never in their wildest dreams could acquire.

After I typed all these funny staff, I found this:

http://learnpythonthehardway.org/book/advice.html

Python: String split and for each

Categories: Python; Tagged with: ; @ April 23rd, 2014 0:49

This example shows how to split string and do for…each in Python:

>>> rawTags = 'TAG1 TAG2 TAG3 TAGB1 TAGB2'
>>> tags = rawTags.split()
>>> tags
['TAG1', 'TAG2', 'TAG3', 'TAGB1', 'TAGB2']
>>> output = ''
>>> for tag in tags:
... output += '"' + tag + '",'
>>> output
'"TAG1","TAG2","TAG3","TAGB1","TAGB2",'

Python: Parse HTML using Beautiful Soup

Categories: Python; Tagged with: ; @ April 6th, 2014 17:24

Requirement:

Get the FX rate from: http://fx.cmbchina.com/hq

Solution:

Use urllib2 to get the html, use BeautifulSoup to parse the html;

Details:

1.  Get html by urllib2:

import urllib2
html = urllib2.urlopen(“
http://fx.cmbchina.com/hq”).read()

2. parse the html using BeautifulSoup:

install it:

$ apt-get install python-bs4 // http://www.crummy.com/software/BeautifulSoup/bs4/doc/#installing-beautiful-soup

Python Code:

from bs4 import BeautifulSoup
soup = BeautifulSoup(html)   # you may check the HTML by: print(soup.prettify()) 
sgRateString = soup.find(id=”realRateInfo”).find_all(‘tr’)[8].find_all(‘td’)[5].get_text()
sgRate = float(sgRateString)
print ‘SG Rate:’, sgRate

All Code:

Links

urllib2 examples:  https://docs.python.org/2/library/urllib2.html#examples

BeautifulSoup Quick Start: http://www.crummy.com/software/BeautifulSoup/bs4/doc/#quick-start

Newer Posts



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.