Formatting .sgm files in Python