繁体   English   中英

将转换后的字节转换为 str 到字节 python

[英]Convert a converted bytes to str to bytes python

如何将转换后的字节转换为 str 到字节 python 简单:

re="Xin chào"
print(re.encode("utf-8"))
# OUTPUT : b'Xin ch\xc3\xa0o'

但我有一个像

print(new_str)
# OUTPUT : Xin ch\xc3\xa0o 

那么我该如何解码“new_str”var

我不完全理解您要做什么,但是如果您想对字符串或变量进行编码,请使用 encode() function:

string = "string"
en = string.encode()
print(en)

例如:如果字符串是“Hello”,则 output 将是 b'Hello'。

如果你想解码:

print (string.decode('utf8', 'strict'))

假设您的new_str var 包含b'Xin ch\xc3\xa0o'所以,

new_str = b'Xin ch\xc3\xa0o'

您可以使用decode()方法将其转换回Xin chào

new_str = b'Xin ch\xc3\xa0o'
print(new_str.decode())

output 将, Xin chào

你是说“解码”吗? 这是来源

string = r"Xin ch\xc3\xa0o"
exec(f"global string;string=b'{string}'")
string = string.decode("utf-8")
print(string)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM