删除Python中某个字符串之前和之后的字符

时间:2016-06-09 18:50:10

标签: python-2.7 base64

我编写了一段代码,使用get请求以文本文件的形式从API接收响应。这个文件包含一个用base64编码的二进制文件,我感兴趣。我希望能够使用Python从文件中仅提取此二进制文件,以便我可以对其进行解码。

我目前的代码是:

    response = requests.get(url, headers=header, verify=False)

    #Decoding response from base64
    decoded_response = base64.b64decode(response)

    #Printing response
    print decoded_response.text

    #Writing response to a file
    f = open("decoded_response.txt","w")
    f = f.read()
    pickle.dump(decoded_response, f)
    f.close()

由于需要删除base64编码字符串开头之前的填充,因此会抛出TypeError。 寻求帮助!

Beginning of file

End of file

1 个答案:

答案 0 :(得分:0)

以下修改过的代码对我有用:

f = open("response.txt","wb")
f.write(json.loads(response.text)['Binary'].decode('base64'))
f.close()