Learning Python

Submitted by olaf on 2014-12-02
Last modified at 2016-05-06

When it comes to scripting, I usually start with a simple shell script. If it gets a bit more complicated, I then move to awk or Perl. But it never hurts to learn another language, if only to gather new insights.

Here’s a simple cheat sheet, a list of Python idioms I found somewhere on the web. Resources include

Looping over command line file input

import fileinput
for line in fileinput.input():
    process(line)

Reading a file at once with file.read

f = open('example.txt')
content = f.read()

Reading a file into a list with file.readlines

f = open('example.txt')
lines = f.readlines()

or

lines = [line.strip() for line in open('example.txt')]

Open a file in user’s home directory, denoted by a tilde ~

filepath = os.path.expanduser('~/example.txt')
f = open(filepath)

Convert bytes to a string

b.decode('iso-8859-1')
b.decode('utf-8')
b.decode()

Matching a regular expression

import re
m = re.search('some(.*)regular\s*expression', source, re.IGNORECASE)
if m:
    s = m.group(1)

Ternary operator (FAQ)

stmt1 if condition else stmt2

Download a web page

import urllib2
page = urllib2.urlopen('http://www.example.com')
# do something with HTML, e.g.
for line in page:
    process(line)

Same with Python 3’s urllib.request

import urllib.request
response = urllib.request.urlopen('http://www.example.com')
for line in response:
    print(line)

Fixing error UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 6: ordinal not in range(128).

Change

print s

to

print s.encode('utf-8')

or whatever encoding you use.

Accessing command line arguments

import sys
sys.argv[1]

Starting a subprocess

import subprocess
retcode = subprocess.call(['ls', '-l'])

Create a pipeline with subprocess. Feed standard output of first command to standard input of the second command. Same as a shell pipeline find -name '*.py' | xargs grep import

import subprocess
p1 = subprocess.Popen(['find', '-name', '*.py'], stdout = subprocess.PIPE)
retcode = subprocess.call(['xargs', 'grep', 'import'], stdin = p1.stdout)

Parsing an HTML document with html.parser

import urllib.request
from html.parser import HTMLParser

class PrintAnchorAttributes(HTMLParser):
    def handle_starttag(self, tag, attrs):
        if tag == 'a':
            for attr in attrs:
                print(attr)

response = urllib.request.urlopen('http://www.example.com')
parser = PrintAnchorAttributes()
for line in response:
    parser.feed(line.decode('utf-8'))

Simple debug logging to a file

import logging
logging.basicConfig(filename = 'debug.log', level = logging.DEBUG)
logging.debug('fingerprint={}'.format(fingerprint))

Common string operations and String Methods

Print without newline

print('Hello, world!', end = '')

and in Python 2

sys.stdout.write('Hello, world!')

or with appended space

print('Hello, world!'),

Boolean values are capitalized! True and False

Reading input from user: input([prompt])

Post a comment

All comments are held for moderation; Markdown and basic HTML formatting accepted. If you want to stay anonymous, leave name, e-mail and website empty.