如何在 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+ 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.