Python: 为什么我的 base64 文件名编码不匹配?

Python: Why is my base64 filename encode not matching what it should be?

我在 Python 中遇到了 base64 情况,我不太明白 -- 当我 运行 我的脚本时:

import rebus
import os

# Set directory
os.chdir('/Users/Ryan/')

# Filename from folder
filename = 'Ryan20160708.txt'

# Create the base64 of the filename
filenameenc = rebus.urlsafe_b64encode(filename)
filebase64 = filenameenc.rstrip()

# Print these for developer
print("Your filename is: " + filename)
print("The base64 encoded filename is: " + filebase64)

我得到以下结果:

Your filename is: Ryan20160708.txt
The base64 encoded filename is: UnlhbjIwMTYwNzA4LnR4dAoK

这个较大的脚本用于 API post,我一直收到 500 错误。那是当我更仔细地查看 base64 名称并意识到它与 https://www.base64encode.org/ 之类的东西不匹配时,它返回 UnlhbjIwMTYwNzA4LnR4dA== 而不是 UnlhbjIwMTYwNzA4LnR4dAoK。就在最后几个字符中。

我唯一注意到的是,当我将我的 base64 放入站点并将其解码回文件名时,它返回正确的文件名 (Ryan20160708.txt),但它有两个换行符在它下面。虽然我已经用 rstrip() 从中删除了任何额外的空格——所以我很困惑。有什么建议吗?

如果你阅读了上面的 PyPI:https://pypi.python.org/pypi/rebus/0.2

您会看到 rebus 用于:

Generate base64-encoded strings consisting of alphanumeric symbols only.

如果您实际上正在寻找 base64 编码,我怀疑这就是您想要的。您可能只需要像这样的 base64:

import base64
import os

# Set directory
os.chdir('/Users/Ryan/')

# Filename from folder
filename = 'Ryan20160708.txt'

# Create the base64 of the filename
filenameenc = base64.b64encode(filename)
filebase64 = filenameenc.rstrip()

# Print these for developer
print("Your filename is: " + filename)
print("The base64 encoded filename is: " + filebase64)

哪个会给你

Your filename is: Ryan20160708.txt
The base64 encoded filename is: 'UnlhbjIwMTYwNzA4LnR4dA=='