Python:为什么我的base64文件名编码与应该的编码不匹配?
在Python中,我有一个base64的情况,我不太清楚--当我运行脚本时:Python:为什么我的base64文件名编码与应该的编码不匹配?,python,base64,Python,Base64,在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) filebas
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文章,我不断收到500
错误。这时我更仔细地研究了base64名称,发现它与诸如on之类的东西不匹配,它返回UnlhbjIwMTYwNzA4LnR4dA=
而不是UnlhbjIwMTYwNzA4LnR4dAoK
。就在最后几个角色里
我注意到的唯一一件事是,当我将base64放入站点并将其解码回文件名时,它会返回正确的文件名(
Ryan20160708.txt
),但它下面有两个换行符。虽然我已经用rstrip()
从中去掉了任何额外的空格,但我还是被卡住了。有什么建议吗?如果您阅读了上面的PyPI:
您将看到rebus用于:
生成由字母数字符号组成的base64编码字符串
只是
如果你真的在寻找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=='
如果您阅读了上面的PyPI: 您将看到rebus用于: 生成由字母数字符号组成的base64编码字符串 只是 如果你真的在寻找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=='
您注意到您正在对编码文本调用
rstrip()
,对吗?您注意到您正在对编码文本调用rstrip()
,对吗?