We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

A Simple YENC Decoder Module

April 2, 2002 | Fredrik Lundh

Note: The code below is a yEnc decoder module for the Python programming language. If you’re looking for something that you can use right away, chances are that you will found what you’re looking for here or here, or via one of the links to the left.

Decode a YENC-encoded message (Python 1.5.2 and later)
import string

yenc42 = string.join(map(lambda x: chr((x-42) & 255), range(256)), "")
yenc64 = string.join(map(lambda x: chr((x-64) & 255), range(256)), "")

# Decode a YENC-encoded message into a list of string fragments.
# Returns None if no YENC body was found.

def yenc_decode(file):
    # find body
    while 1:
        line = file.readline()
        if not line:
            return None
        if line[:7] == "=ybegin":
    # extract data
    buffer = []
    while 1:
        line = file.readline()
        if not line or line[:5] == "=yend":
        if line[-2:] == "\r\n":
            line = line[:-2]
        elif line[-1:] in "\r\n":
            line = line[:-1]
        data = string.split(line, "=")
        buffer.append(string.translate(data[0], yenc42))
        for data in data[1:]:
            data = string.translate(data, yenc42)
            buffer.append(string.translate(data[0], yenc64))
    return buffer