【问题标题】:python3 bytes replace %3D by = in the end of file as fast as it possiblepython3 字节尽可能快地在文件末尾用 = 替换 %3D
【发布时间】:2018-05-12 18:06:19
【问题描述】:

我有一个字节对象,它实际上是dataurl 格式的文件。大约 500 KB。

我需要删除 37 字节的标头(我使用切片制作)并在文件末尾将 %3D 替换为 =(此序列可以找到 0-2 次)。

Urllib.parse 更改对象中的所有条目。

有处理这个对象的好方法吗?

    content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
    post_body = self.rfile.read(content_length) # <--- Gets the data itself
    print(len(post_body))
    with open("1111", "wb") as fd:
        fd.write(post_body)

    post_body = post_body[37:len(post_body)]

    with open("decoded.png", "wb") as fh:
        fh.write(base64.decodebytes(post_body))

在最后一行,我有一个问题。

= 可能会添加字符以使最后一个块包含四个 base64 字符。但是在发帖请求中,我有%3D 而不是=

【问题讨论】:

  • 你能显示你试过的代码吗?还尝试给出一个最小的可重现示例。我建议阅读 SO 的 How to Ask Guidelines
  • 我在解析 url 之前使用了解码。这是一个错误。

标签: python parsing urllib urldecode


【解决方案1】:

在我看来,您需要“取消引用”url 转义 (%xx) 符号。

Python 有一个函数,在 python2.7 中是urllib.unquote,在 python3 中是urllib.parse.unquote。示例用法为:

from urllib.parse import unquote

post_body = unquote(post_body[37:])
  # my_list[i:] is short for my_list[i:len(my_list)]

但是,我不知道您是否只想将其应用于最后一个字节,或者仅在字节以 %3D... 结尾时才应用,您可以使用适用于字符串的 .endswith() 和字节相同:

my_bytes.endswith('%3D')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    • 2012-02-03
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 2013-03-20
    • 1970-01-01
    相关资源
    最近更新 更多