Check if a string is encoded in base64 using Python

前端 未结 10 2032
离开以前
离开以前 2021-02-05 01:56

Is there a good way to check if a string is encoded in base64 using Python?

10条回答
  •  时光说笑
    2021-02-05 02:22

    if the length of the encoded string is the times of 4, it can be decoded

    base64.encodestring("whatever you say").strip().__len__() % 4 == 0
    

    so, you just need to check if the string can match something like above, then it won't throw any exception(I Guess =.=)

    if len(the_base64string.strip()) % 4 == 0:
        # then you can just decode it anyway
        base64.decodestring(the_base64string)
    

提交回复
热议问题