In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to convert between bytes byte strings and string strings in Python, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.
Background
In the work, we often encounter the problem of conversion between bytes and string, so make a record.
Bytes is only responsible for storing data in the form of byte sequences (binary form), regardless of whether the data itself is pictures, text, video, and so on. If you need to use and display it, follow the corresponding parsing rules, and you can get the corresponding type of data. For common string types, you only need to use the corresponding character encoding format to get the contents of the string.
Code
Take a look at the code first.
#! / user/bin/env python# coding=utf-8 "" @ project: csdn@author: huyi@file: byte_to_string.py@ide: PyCharm@time: 2021-12-23 11:47:45 "# No character set b1 = b'I love u, baby'print ('b1weather, b1) print (b1 [:-3]) # specify character set b2 = bytes (' beautiful day / ', encoding='UTF-8') print ('b2' B2) will the conversion of # string to bytesstr1 = 'will meta-universe lead to the stagnation of human civilization?' B3 = str1.encode ('UTF-8') print (' b3, b3) # bytes to string method 1str2 = str (b3, 'UTF-8') print (' str2', str2) # bytes to string method 1str3 = b3.decode ('UTF-8') print (' str3', str3) code description:
1. You can define bytes with a lowercase b before the string, but it is not recommended. It is recommended to use the definition method of b2, and you can adjust the character encoding.
2. The data of string type can be converted into bytes according to character encoding through the encode method.
3. Bytes can also specify character encoding or decode method through the construction of str to convert bytes into a string.
Verify that PyDev console: starting.Python 3.6.13 | Anaconda, Inc. | (default, Mar 16 2021, 11:37:27) [MSC v.1916 64 bit (AMD64)] on win32runfile ('DWR hand SpyderThink wdir='D:/spyder/csdn/tool') b1 b'I love u, baby'b'I love u B'b2 b'\ xe4\ xbb\ x8a\ xe5\ xa9\ xe5\ xa4\ xa9\ xb0\ x94\ xe7\ X9c\ X9f\ xe5\ xa5\ xbd/\ xe5\ x93\ xe5\ x93\ x88'b3 b'\ xe5\ x83\ xe5\ xae\ x87\ xe5\ xae\ xe4\ xbc\ X9a\ xe5\ xaf\ xbc\ xe8\ X87\ xb4 xe4\ xba\ xba\ xe7 xb1\ xbb\ xe6\ x96\ x87\ xe6\ x98\ X8e Will\ x9a\ x84\ xe5\ x81\ x9c\ xe6\ xbb\ x9e\ xe5\ x90\ x97\ xef\ xbc\ x9f'str2 meta-universe lead to the stagnation of human civilization? Will str3 meta-universe lead to the stagnation of human civilization? Attached: the difference between bytes and string
1.python bytes is also called byte sequence, not character. Value range 0
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.