无法通过正则表达式从 python3 中的字节中删除字符串
can't remove a string from bytes in python3 by regex
*我在 python 中使用 re.sub 时出错 3
**我的代码是:
chunk = re.sub(b'-----------------------------(.+)--\r\n', '', chunk
我有这个错误:
Type Error: sequence item 0: `expected` a bytes-like object, str found
它在 python2 中有效,但在 python3.4 中无效。
另外我用tornadofreamwork
请帮我解决这个问题。
替换部分也必须是字节串。
chunk = re.sub(b'-----------------------------(.+)--\r\n', b'', chunk)
示例:
>>> chunk = b'-----------------------------5313032314004\r\nContent-Disposition: form-data; name="file"; filename="4.jpg"\r\nContent-Type: image/jpeg\r\n\r\n\xff\xd8\xff'
>>> re.sub(b'-', b'', chunk)
b'5313032314004\r\nContentDisposition: formdata; name="file"; filename="4.jpg"\r\nContentType: image/jpeg\r\n\r\n\xff\xd8\xff'
*我在 python 中使用 re.sub 时出错 3 **我的代码是:
chunk = re.sub(b'-----------------------------(.+)--\r\n', '', chunk
我有这个错误:
Type Error: sequence item 0: `expected` a bytes-like object, str found
它在 python2 中有效,但在 python3.4 中无效。 另外我用tornadofreamwork 请帮我解决这个问题。
替换部分也必须是字节串。
chunk = re.sub(b'-----------------------------(.+)--\r\n', b'', chunk)
示例:
>>> chunk = b'-----------------------------5313032314004\r\nContent-Disposition: form-data; name="file"; filename="4.jpg"\r\nContent-Type: image/jpeg\r\n\r\n\xff\xd8\xff'
>>> re.sub(b'-', b'', chunk)
b'5313032314004\r\nContentDisposition: formdata; name="file"; filename="4.jpg"\r\nContentType: image/jpeg\r\n\r\n\xff\xd8\xff'