import base64


def _pem_encode_csr(csr):
    """Encodes the CSR in PEM format"""
    b64_csr = base64.b64encode(csr).decode('ascii')
    b64rn_csr = '\r\n'.join(b64_csr[pos:pos+64] for pos in range(0, len(b64_csr), 64))
    pem_csr = '-----BEGIN NEW CERTIFICATE REQUEST-----\r\n'
    pem_csr += b64rn_csr
    pem_csr += '\r\n-----END NEW CERTIFICATE REQUEST-----'
    return pem_csr