Archive
Where does a page redirect to?
Question
We have a page that redirects to another page. How to figure out where the redirection points to?
Answer
import urllib s = "https://pythonadventures.wordpress.com?random" # returns a random post page = urllib.urlopen(s) print page.geturl() # e.g. http:// pythonadventures.wordpress.com/2010/10/08/python-challenge-1/
Credits
I found it in this thread.
Update (20121202)
With requests:
>>> import requests
>>> r = requests.get('https://pythonadventures.wordpress.com?random')
>>> r.url
u'https://pythonadventures.wordpress.com/2010/09/30/create-import-module/'
Reading and writing a file
Here is a mini cheat sheet for reading and writing a text file.
Read a text file line by line and write each line to another file (copy):
f1 = open('./in.txt', 'r')
to = open('./out.txt', 'w')
for line in f1:
to.write(line)
f1.close()
to.close()
Variations:
text = f.read() # read the entire file line = f.readline() # read one line at a time lineList = f.readlines() # read the entire file as a list of lines
unicode to ascii
Problem
I had the following unicode string: “Kellemes Ünnepeket!” that I wanted to simplify to this: “Kellemes Unnepeket!”, that is strip “Ü” to “U”. Furthermore, most of the strings were normal ascii, only some of them were in unicode.
Solution
import unicodedata
title = ... # get the string somehow
try:
# if the title is a unicode string, normalize it
title = unicodedata.normalize('NFKD', title).encode('ascii','ignore')
except TypeError:
# if it was not a unicode string => OK, do nothing
pass
Credits
I used the following resources:
- http://www.peterbe.com/plog/unicode-to-ascii
- http://stackoverflow.com/questions/196345/how-to-check-if-a-string-in-python-is-in-ascii
Using MySQL from Python
Problem
You want to interact with a MySQL database from your Python script.
Solution
First of all, you need to install the following package:
sudo apt-get install python-mysqldb
Then try the following basic script to check if everything is OK:
#!/usr/bin/env python
import MySQLdb
conn = MySQLdb.connect (host = "localhost",
user = "testuser",
passwd = "testpass",
db = "test")
cursor = conn.cursor ()
cursor.execute ("SELECT VERSION()")
row = cursor.fetchone ()
print "server version: ", row[0]
cursor.close ()
conn.close ()
Example:
We have a .csv file with two columns: symbol and name. Iterate through the lines and insert each line in a database table as a record.
#!/usr/bin/env python
import MySQLdb
f1 = open('./NYSE.csv', 'r')
# A line looks like this:
# ZLC; Zale Corporation
conn = MySQLdb.connect(host = "localhost",
user = "user",
passwd = "passwd",
db = "table")
cursor = conn.cursor()
for line in f1:
pieces = map(str.strip, line.split(';'))
#print "'%s' => '%s'" % (pieces[0], pieces[1])
query = "INSERT INTO symbol_name (symbol, name) VALUES (\"%s\", \"%s\")" % (pieces[0], pieces[1])
#print query
cursor.execute(query)
f1.close()
conn.commit()
cursor.close ()
conn.close ()
Links
There are lots of Python-MySQL tutorials on the net. Let’s see some of them:
- http://www.kitebird.com/articles/pydbapi.html
- http://zetcode.com/databases/mysqlpythontutorial/
- http://www.tutorialspoint.com/python/python_database_access.htm
