如何将字节转换为Python字符串
在本文中,我们将展示如何在Python中将字节转换为字符串。以下是完成此任务的各种方法:
- 使用decode()函数
-
使用str()函数
-
使用codecs.decode()函数
-
使用pandas库
使用decode()函数
在Python中,内置的 decode() 方法用于将字节转换为字符串。
步骤
以下是执行所需任务的算法/步骤:
- 创建一个变量来存储输入的字节字符串数据。
-
打印输入数据。
-
使用 type() 函数(返回对象的数据类型)打印输入数据的类型。
-
使用 decode() 函数将输入的字节转换为Python字符串。
-
打印输出数据。
-
使用 type() 函数在将输入字节转换为Python字符串后打印输出数据的类型。
示例
以下程序使用decode()函数将输入的字节转换为Python字符串-
# input bytes
inputData = b'TutorialsPoint'
# printing input bytes data
print('Input data:', inputData)
# printing the type of input data
print(type(inputData))
# converting the input bytes to a python string using decode() function
result = inputData.decode()
# printing the result after decoding the bytes
print('Output data:', result)
# printing the type of output data(result) after conversion
print(type(result))
输出
执行以上程序时,将生成以下输出:
Input data: b'TutorialsPoint'
<class 'bytes'>
Output data: TutorialsPoint
<class 'str'>
使用str()函数
str()函数返回对象的字符串格式,即将其转换为字符串形式。
步骤
以下是执行所需任务的算法/步骤:
- 创建一个变量来存储输入的字节字符串数据。
-
打印输入数据。
-
使用type()函数(返回对象的数据类型)打印输入数据的类型。
-
使用str()函数将输入字节转换为Python字符串,将输入数据和’UTF-8’作为参数传递给它。
-
打印输出数据。
-
使用type()函数在将输入字节转换为Python字符串后打印输出数据的类型。
示例
以下程序使用str()函数将输入字节转换为Python字符串:
# input bytes
inputData = b'TutorialsPoint'
# printing input bytes data
print('Input data:', inputData)
# printing the type of input data
print(type(inputData))
# converting the input bytes to a string using str() function
result = str(inputData, 'UTF-8')
# printing the result after decoding the bytes
print('Output data:', result)
# printing the type of output data(result) after conversion
print(type(result))
输出
在执行上述程序时,将生成以下输出:
Input data: b'TutorialsPoint'
<class 'bytes'>
Output data: TutorialsPoint
<class 'str'>
使用codecs.decode()函数
codecs模块也可以用来将字节数据类型转换为字符串。
codecs模块的decode()方法将二进制字符串解码为普通形式。
步骤
执行所需任务的算法/步骤如下:
- 使用import关键字导入codecs模块。
-
创建一个变量来存储输入的字节字符串数据。
-
打印输入数据。
-
使用type()函数(返回对象的数据类型)来打印输入数据的类型。
-
使用codecs模块的decode()函数将输入字节转换为Python字符串。
-
打印输出数据。
-
使用type()函数打印输入字节转换为Python字符串后的输出数据类型。
示例
以下程序使用codecs.decode()函数将输入字节转换为Python字符串-
# importing codecs module
import codecs
# input bytes
inputData = b'TutorialsPoint'
# printing input bytes data
print('Input data:', inputData)
# printing the type of input data
print(type(inputData))
# converting the input bytes to a python string using
# codecs.decode() function
result = codecs.decode(inputData)
# printing the result after decoding the bytes
print('Output data:', result)
# printing the type of output data(result) after conversion
print(type(result))
输出
执行上述程序后,将生成以下输出结果−
Input data: b'TutorialsPoint'
<class 'bytes'>
Output data: TutorialsPoint
<class 'str'>
使用pandas库
语法
str.decode()
通过在 pandas 中的数据帧中调用str.decode()
函数,您可以快速将字节转换为字符串。
默认情况下,Python的字符编码通常是UTF-8。
步骤
以下是执行所需任务的算法/步骤:
- 使用
import
关键字导入pandas模块。 -
创建一个变量来存储输入字典,并给它一些随机的键值对(在这里我们将字节作为值传递)。
-
使用
DataFrame()
函数创建上述输入字典的pandas数据帧。 -
使用
decode()
函数将衣服列的值从字节转换为字符串,并将此结果数据帧存储在一个变量中。 -
在将值从字节转换为字符串后,打印结果数据帧。
示例
以下程序将返回经过将其值从字节转换为字符串后的pandas数据帧:
# importing pandas module
import pandas as pd
# input dictionary
inputDict = {'clothes' : [ b'shirt', b'pant', b'tshirt', b'cap']}
# Creating pandas dataframe of the above dictionary
dataframe = pd.DataFrame(data=inputDict)
# Converting the dictionary values(bytes) to string
result = dataframe['clothes'].str.decode("utf-8")
# printing the resultant dataframe after converting the values from bytes to string
print(result)
输出
执行上述程序后,将生成以下输出 –
0 shirt
1 pant
2 tshirt
3 cap
Name: clothes, dtype: object
结论
在本文中,我们学习了使用四种不同的方法将字节转换为Python字符串。我们还学习了将Python字典转换为pandas数据帧的方法。我们学习了如何使用decode()函数将列的值转换为字符串。