【问题标题】:Python- encode base64 print new linePython-编码base64打印新行
【发布时间】:2022-01-09 16:47:15
【问题描述】:

我想尝试用base64 加密我的python 代码。 但是当我使用\n 时出现错误:

print("hello 
IndentationError: unexpected indent

这是我使用的代码:

import base64

def encode(data):
    try:
        # Standard Base64 Encoding
        encodedBytes = base64.b64encode(data.encode("utf-8"))
        return str(encodedBytes, "utf-8")
    except:
        return ""
    
def decode(data):
    try:
        message_bytes = base64.b64decode(data)
        return message_bytes.decode('utf-8')
    except:
        return ""

your_code = encode("""
    print("hello \n world")

""")

exec(decode(your_code))

我可以使用两次print() 函数而不是\n,但是有没有办法使用\n

希望你能帮帮我

【问题讨论】:

    标签: python base64 newline


    【解决方案1】:

    首先,您必须删除 your_code 部分中的缩进。其次,您必须将\n 替换为\\n

    import base64
    
    
    def encode(data):
        try:
            # Standard Base64 Encoding
            encodedBytes = base64.b64encode(data.encode("utf-8"))
            return str(encodedBytes, "utf-8")
        except:
            return ""
    
    
    def decode(data):
        try:
            message_bytes = base64.b64decode(data)
            return message_bytes.decode('utf-8')
        except:
            return ""
    
    
    your_code = encode("""
    print("hello \\n world")
    
    """)
    
    exec(decode(your_code))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-21
      • 1970-01-01
      • 2014-10-21
      • 2019-09-28
      • 2019-01-18
      • 1970-01-01
      相关资源
      最近更新 更多