【Python】TypeError: a bytes-like object is required, not 'str'
1、報錯
自己python3.+,使用別人python2.的程式碼報錯:
rs = pow(int(binascii.hexlify(text), 16), int(pubKey, 16), int(modulus, 16))
TypeError: a bytes-like object is required, not 'str'
2、原因
因為python3是bytes-like的,只能把它轉為str
3、解決
# bytes object b = b"example" # str object s = "example" # str to bytes bytes(s, encoding = "utf8") # bytes to str str(b, encoding = "utf-8") # an alternative method # str to bytes str.encode(s) # bytes to str bytes.decode(b)
4、參考
相關推薦
【Python】TypeError: a bytes-like object is required, not 'str'
1、報錯 自己python3.+,使用別人python2.的程式碼報錯: rs = pow(int(binascii.hexlify(text), 16), int(pubKey, 16), int
Python異常:TypeError: a bytes-like object is required, not 'str'
Python異常:TypeError: a bytes-like object is required, not ‘str’ import json str = [{"userName" : "Use
【爬坑】Python 3.6 在 Socket 程式設計時出現型別錯誤 TypeError: a bytes-like object is required, not 'str'
1. 問題描述 Python 3.6 在 Socket 程式設計時出現錯誤如下 Traceback (most recent call last): File "F:/share/IdeaProjects/test/mypython/test/test10_tcpcli
Python 3.5.2 TypeError: a bytes-like object is required, not 'str’問題解決方案
con 指定 err byte 讀取 字節 code 解決方案 錯誤 運行環境Mac Python 3.5.2 Q: http_response = """\ HTTP/1.1 200 OK Hello, World! """ client_connection.se
Python異常:TypeError: a bytes-like object is required, not 'str'
Python異常:TypeError: a bytes-like object is required, not ‘str’ import json str = [{"userName" : "UserPython", "age" : 20}, (2, 3), 1] with o
TypeError: a bytes-like object is required, not 'str'
ef7 error ref date bytes wiki err obj target python bytes和str兩種類型轉換的函數encode(),decode() str通過encode()方法可以編碼為指定的bytes 反過來,如果我們從網絡或磁盤上讀取了
Socket send() method throws TypeError: a bytes-like object is required, not 'str'
error: ring net peer 函數 搜索 string類型 unicode 區分 python3 socket編程,發送data數據,會遇到需要bytes類型,而不是str字符串的錯誤 例如: import socket mysock = socket
python3 中pickle.load TypeError: a bytes-like object is required, not 'str'
bug原因:Python2 和 3 的字串相容問題,資料檔案是在Python2下是序列化的,所以使用Python3讀取時,需要將‘str’轉化為'bytes'。 Python2 中的寫法: # 獲取漢字label對映表 def get_label_dict():
UDP通訊解決a bytes-like object is required, not 'str' 問題
簡介:最近在進行網路通訊的學習時候遇到了這個問題,使用的版本是Python3.5.2,啟動伺服器端,再啟動客戶端通訊時候遇到該問題,並且直接出錯無法繼續執行 服務端的程式是一個UDP通訊程式 import socket s=socket.socket(socket.AF
python3 TypeError: a bytes-like object is required
python運行telnetlib的時候報錯:TypeError: a bytes-like object is required, not ‘str’,原因是因為python2和python3的版本的差異。在python2中可正常運行,而python3最重要的新特性也是對文本和二進制數據做了更清晰的區分。文
【python】TypeError: 'zip' object is not subscriptable解決方法
原始碼: for dog,cat in zip(dp.dogs,dp.cats)[:1000]: #省略程式碼 報錯: TypeError: 'zip' object is not subscriptable 解決方法:使用list包裝zip物件,如下, fo
TypeError: cannot use a string pattern on a bytes-like object
dsm 模塊 odi nbsp mode IE python2 play ray 一勞永逸解決:TypeError: cannot use a string pattern on a bytes-like object TypeError: canno
python Bug記錄-int() argument must be a string, a bytes-like object or a number, not 'dict'
程式碼如下: error_msg = "" if request.method == "POST": new_dayincome = request.POST.get("dayincome_time", None) total_bath =
一勞永逸解決:TypeError: cannot use a string pattern on a bytes-like object
TypeError: cannot use a string pattern on a bytes-like object 最近寫程式碼,python2和python3之間切換,難免會碰到一些問題,有些方法比如re模組的findall要求傳入的是字串格式的引數
statsmodels.tsa.arima_model預測時報錯TypeError: int() argument must be a string, a bytes-like object or a number, not 'Timestamp'
進行 時報 參數 csv 別人 間隔 get req ice 在 python 中用 statsmodels創建 ARIMA 模型進行預測時間序列: import pandas as pd import statsmodels.api as sm df = pd.read
can't use a string pattern on a bytes-like object錯誤和must be str, not bytes錯誤
哎,新手使用Python真是痛苦,步步艱辛步步淚!寫了四行程式,出來兩個錯誤。下面記錄錯誤和解決方法 ======================================================================================
TypeError: 'bytes' object is not callable 解決方法
原始碼如下,定義的本地變數get_time和get_time()外部引入方法重名,呼叫get_time(),被當成了本地變數get_time= b'\d+ .\d+',變數不可被呼叫(函式才可呼叫)。 if __name__ == '__main__': get_time= b'\d+\
python爬蟲問題:TypeError: cannot use a string pattern on a bytes-like objec
Python3.x在學到爬蟲是需要注意不同於Python2.x需要將html進行解碼: import urllib import re def download(url,user_agent='XD',num_retries=2): print
【python】報錯:TypeError: 'builtin_function_or_method' object is unsubscriptable的解決方法
出現這個報錯的原因其實很簡單,就是將小括號'()'寫成了中括號‘[]' 下面來看一個例子: 當一個字典裡面嵌套了字典和列表的時候,再通過字典多層呼叫,將get函式後的小括號寫成了中括號。如下面程式碼第二行: 1.def lookup(data,label,name):
【Python】07、python內置數據結構之字符串及bytes
str 字符串 一、字符串1、定義和初始化In [4]: s = "hello python" In [4]: s = "hello python" In [5]: s Out[5]: ‘hello python‘ In [6]: s = ‘hello python‘ In [7]: s Out