I think these lines could be simplified and be easier to follow.  Maybe something like this (untested)?

    if isinstance(msg, basestring):
        out.write(u'\n\n;{msg}\n{msg}\n'.format(msg=msg))
    else:
        out.write(u'\n\n# {comment}\n;{msg}\n{msg}\n'.format(comment=msg[0], msg=msg[1]))