如何在 python 字符串中删除 b 前缀?
- 2025-03-05 09:18:00
- admin 原创
- 65
问题描述:
我有一个带有 b 前缀的字符串:
b'I posted a new photo to Facebook'
我认为b
这是一个字节字符串。
我该如何删除这个b
前缀?我试过:
b'I posted a new photo to Facebook'.encode("utf-8").decode("utf-8")
但这会出现错误:
UnicodeEncodeError: 'charmap' codec can't encode characters in position 64-65: character maps to <undefined>
解决方案 1:
decode
产生bytes
一个str
:
b = b'1234'
print(b.decode('utf-8')) # '1234'
解决方案 2:
您正在打印的对象不是字符串,而是字节文字bytes
对象。
考虑通过键入字节文字(从字面上定义字节对象而不实际使用字节对象,例如键入 b'')并将其转换为以 utf-8 编码的*字符串对象来创建字节对象。 (请注意,这里的转换意味着解码*)
byte_object= b"test" # byte object by literally typing characters
print(byte_object) # Prints b'test'
print(byte_object.decode('utf8')) # Prints "test" without quotations
我们只是应用了这个.decode(utf8)
功能。
字符串文字由以下词汇定义描述:
https://docs.python.org/3.3/reference/lexical_analysis.html#string-and-bytes-literals
stringliteral ::= [stringprefix](shortstring | longstring)
stringprefix ::= "r" | "u" | "R" | "U"
shortstring ::= "'" shortstringitem* "'" | '"' shortstringitem* '"'
longstring ::= "'''" longstringitem* "'''" | '"""' longstringitem* '"""'
shortstringitem ::= shortstringchar | stringescapeseq
longstringitem ::= longstringchar | stringescapeseq
shortstringchar ::= <any source character except "\" or newline or the quote>
longstringchar ::= <any source character except "\">
stringescapeseq ::= "\" <any source character>
bytesliteral ::= bytesprefix(shortbytes | longbytes)
bytesprefix ::= "b" | "B" | "br" | "Br" | "bR" | "BR" | "rb" | "rB" | "Rb" | "RB"
shortbytes ::= "'" shortbytesitem* "'" | '"' shortbytesitem* '"'
longbytes ::= "'''" longbytesitem* "'''" | '"""' longbytesitem* '"""'
shortbytesitem ::= shortbyteschar | bytesescapeseq
longbytesitem ::= longbyteschar | bytesescapeseq
shortbyteschar ::= <any ASCII character except "\" or newline or the quote>
longbyteschar ::= <any ASCII character except "\">
bytesescapeseq ::= "\" <any ASCII character>
解决方案 3:
您需要对其进行解码才能将其转换为字符串。在此处查看有关 python3 中的字节文字的答案。
b'I posted a new photo to Facebook'.decode('utf-8')
# 'I posted a new photo to Facebook'
解决方案 4:
如何删除b' '
python 中解码的字符串的字符:
import base64
a='cm9vdA=='
b=base64.b64decode(a).decode('utf-8')
print(b)
解决方案 5:
在装有 django 2.0 的 python 3.6 上,对字节文字进行解码无法按预期工作。是的,当我打印它时,我得到了正确的结果,但是b'value'
即使你正确打印,仍然存在。
这就是我正在编码的内容
uid': urlsafe_base64_encode(force_bytes(user.pk)),
这就是我正在解码的内容:
uid = force_text(urlsafe_base64_decode(uidb64))
这是 django 2.0 所说的:
urlsafe_base64_encode(s)[source]
使用 base64 对字节串进行编码以用于 URL,并删除所有尾随的等号。
urlsafe_base64_decode(s)[source]
解码 base64 编码的字符串,并重新添加可能已被删除的尾随等号。
这是我的 account_activation_email_test.html 文件
{% autoescape off %}
Hi {{ user.username }},
Please click on the link below to confirm your registration:
http://{{ domain }}{% url 'accounts:activate' uidb64=uid token=token %}
{% endautoescape %}
这是我的控制台响应:
内容类型:text/plain;字符集=“utf-8” MIME 版本:1.0 内容传输编码:7bit 主题:激活您的 MySite 帐户 来自:webmaster@localhost 至:testuser@yahoo.com日期:2018 年 4 月 20 日星期五 06:26:46 -0000 消息 ID:<152420560682.16725.4597194169307598579@Dash-U>
嗨,testuser,
请点击以下链接确认您的注册:
http://127.0.0.1:8000/activate/b'MjU'/4vi-fasdtRf2db2989413ba/
如你看到的uid = b'MjU'
预期的uid = MjU
在控制台中测试:
$ python
Python 3.6.4 (default, Apr 7 2018, 00:45:33)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
>>> from django.utils.encoding import force_bytes, force_text
>>> var1=urlsafe_base64_encode(force_bytes(3))
>>> print(var1)
b'Mw'
>>> print(var1.decode())
Mw
>>>
经过调查,似乎它与 python 3 有关。我的解决方法很简单:
'uid': user.pk,
我在激活函数中将其作为 uidb64 接收:
user = User.objects.get(pk=uidb64)
瞧:
Content-Transfer-Encoding: 7bit
Subject: Activate Your MySite Account
From: webmaster@localhost
To: testuser@yahoo.com
Date: Fri, 20 Apr 2018 20:44:46 -0000
Message-ID: <152425708646.11228.13738465662759110946@Dash-U>
Hi testuser,
Please click on the link below to confirm your registration:
http://127.0.0.1:8000/activate/45/4vi-3895fbb6b74016ad1882/
现在它运行良好。
解决方案 6:
假设您不想像其他人在这里建议的那样立即再次对其进行解码,您可以将其解析为字符串,然后只删除前导'b
和尾随'
。
扫码咨询,免费领取项目管理大礼包!