如何將位元組轉換為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() 函式(返回物件的 資料型別)來列印輸入資料的型別。

  • 透過將輸入資料和'UTF-8'作為引數傳遞給它,使用str() 函式將輸入位元組轉換為Python字串。

  • 列印輸出資料。

  • 使用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()

透過在列上呼叫str.decode() 函式,可以快速將pandas中正在處理的資料框中的位元組轉換為字串。

預設情況下,Python 字元編碼通常為UTF-8。

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 使用import關鍵字匯入pandas 模組。

  • 建立一個變數來儲存輸入字典,並賦予它一些隨機的鍵值對。(這裡我們將位元組作為值傳遞)

  • 使用DataFrame()函式建立上述輸入字典的pandas資料框。

  • 使用decode()函式將clothes列的值從位元組轉換為字串,並將此結果資料框儲存在一個變數中。

  • 列印將值從位元組轉換為字串後的結果資料框

示例

下面的程式在將值從位元組轉換為字串後返回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()函式將列值轉換為字串。

更新於:2022年10月28日

11K+ 瀏覽量

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告