如何在python中将文本编码为base64

时间:2014-04-18 23:44:30

标签: python python-3.x encoding base64

我正在尝试将文本字符串编码为base64。

我试过这样做:

name = "your name"
print('encoding %s in base64 yields = %s\n'%(name,name.encode('base64','strict')))

但是这给了我以下错误:

LookupError: 'base64' is not a text encoding; use codecs.encode() to handle arbitrary codecs

我该怎么做呢? (使用Python 3.4)

8 个答案:

答案 0 :(得分:57)

请记住导入base64,并且b64encode将字节作为参数。

import base64
base64.b64encode(bytes('your string', 'utf-8'))

答案 1 :(得分:13)

1)这在Python 2中没有导入的情况下工作:

>>>
>>> 'Some text'.encode('base64')
'U29tZSB0ZXh0\n'
>>>
>>> 'U29tZSB0ZXh0\n'.decode('base64')
'Some text'
>>>
>>> 'U29tZSB0ZXh0'.decode('base64')
'Some text'
>>>

(虽然这在Python3中不起作用)

2)在Python 3中,您必须导入base64并执行base64.b64decode(' ...') - 也可以在Python 2中使用。

答案 2 :(得分:10)

事实证明这很重要,可以获得it's own module ...

import base64
base64.b64encode(b'your name')  # b'eW91ciBuYW1l'
base64.b64encode('your name'.encode('ascii'))  # b'eW91ciBuYW1l'

答案 3 :(得分:2)

兼容py2和py3

import six
import base64

def b64encode(source):
    if six.PY3:
        source = source.encode('utf-8')
    content = base64.b64encode(source).decode('utf-8')

答案 4 :(得分:2)

对于py3,base64 encodedecode字符串:

import base64

def b64e(s):
    return base64.b64encode(s.encode()).decode()


def b64d(s):
    return base64.b64decode(s).decode()

答案 5 :(得分:1)

您当然可以使用base64模块,也可以使用codecs模块(错误消息中提到)进行二进制编码(表示非标准和非文本编码) )。

例如:

import codecs
my_bytes = b"Hello World!"
codecs.encode(my_bytes, "base64")
codecs.encode(my_bytes, "hex")
codecs.encode(my_bytes, "zip")
codecs.encode(my_bytes, "bz2")

这对于大数据很有用,因为您可以将它们链接以获得压缩的和json可序列化的值:

my_large_bytes = my_bytes * 10000
codecs.decode(
    codecs.encode(
        codecs.encode(
            my_large_bytes,
            "zip"
        ),
        "base64"),
    "utf8"
)

参考:

答案 6 :(得分:0)

使用以下代码:

import base64

#Taking input through the terminal.
welcomeInput= raw_input("Enter 1 to convert String to Base64, 2 to convert Base64 to String: ") 

if(int(welcomeInput)==1 or int(welcomeInput)==2):
    #Code to Convert String to Base 64.
    if int(welcomeInput)==1:
        inputString= raw_input("Enter the String to be converted to Base64:") 
        base64Value = base64.b64encode(inputString.encode())
        print "Base64 Value = " + base64Value
    #Code to Convert Base 64 to String.
    elif int(welcomeInput)==2:
        inputString= raw_input("Enter the Base64 value to be converted to String:") 
        stringValue = base64.b64decode(inputString).decode('utf-8')
        print "Base64 Value = " + stringValue

else:
    print "Please enter a valid value."

答案 7 :(得分:0)

即使在对base64编码的字符串调用base64.b64decode之后,似乎也必须调用encode()函数来使用实际的字符串数据。因为永远不会忘记它总是返回字节文字。

import base64
conv_bytes = bytes('your string', 'utf-8')
print(conv_bytes)                                 # b'your string'
encoded_str = base64.b64encode(conv_bytes)
print(encoded_str)                                # b'eW91ciBzdHJpbmc='
print(base64.b64decode(encoded_str))              # b'your string'
print(base64.b64decode(encoded_str).decode())     # your string
相关问题