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 open("jsonTest.txt", "wb") as fp:
json.dump(str, fp = fp)
異常分析:open(“jsonTest.txt”, “wb”)指定檔案以二進位制寫模式開啟,所以需要以二進位制寫入,不能寫入字串,而上面程式碼中str是一個列表字串,所以報異常。
解決方法:因為上面程式碼中str是列表字串,所以將open()函式中的開啟檔案方式改為“w”寫模式即可
import json
str = [{"userName" : "UserPython", "age" : 20}, (2, 3), 1]
with open("jsonTest.txt", "w") as fp:
json.dump(str, fp = fp)
相關推薦
Python異常:TypeError: a bytes-like object is required, not 39;str39;
Python異常:TypeError: a bytes-like object is required, not ‘str’ import json str = [{"userName" : "UserPython", "age" : 20}, (2, 3), 1] with o
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.5.2 TypeError: a bytes-like object is required, not 39;str’問題解決方案
con 指定 err byte 讀取 字節 code 解決方案 錯誤 運行環境Mac Python 3.5.2 Q: http_response = """\ HTTP/1.1 200 OK Hello, World! """ client_connection.se
【爬坑】Python 3.6 在 Socket 程式設計時出現型別錯誤 TypeError: a bytes-like object is required, not 39;str39;
1. 問題描述 Python 3.6 在 Socket 程式設計時出現錯誤如下 Traceback (most recent call last): File "F:/share/IdeaProjects/test/mypython/test/test10_tcpcli
【Python】TypeError: a bytes-like object is required, not 'str'
1、報錯 自己python3.+,使用別人python2.的程式碼報錯: rs = pow(int(binascii.hexlify(text), 16), int(pubKey, 16), int
TypeError: a bytes-like object is required, not 39;str39;
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 39;str39;
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 39;str39;
bug原因:Python2 和 3 的字串相容問題,資料檔案是在Python2下是序列化的,所以使用Python3讀取時,需要將‘str’轉化為'bytes'。 Python2 中的寫法: # 獲取漢字label對映表 def get_label_dict():
python3 TypeError: a bytes-like object is required
python運行telnetlib的時候報錯:TypeError: a bytes-like object is required, not ‘str’,原因是因為python2和python3的版本的差異。在python2中可正常運行,而python3最重要的新特性也是對文本和二進制數據做了更清晰的區分。文
UDP通訊解決a bytes-like object is required, not 'str' 問題
簡介:最近在進行網路通訊的學習時候遇到了這個問題,使用的版本是Python3.5.2,啟動伺服器端,再啟動客戶端通訊時候遇到該問題,並且直接出錯無法繼續執行 服務端的程式是一個UDP通訊程式 import socket s=socket.socket(socket.AF
can't use a string pattern on a bytes-like object錯誤和must be str, not bytes錯誤
哎,新手使用Python真是痛苦,步步艱辛步步淚!寫了四行程式,出來兩個錯誤。下面記錄錯誤和解決方法 ======================================================================================
一勞永逸解決: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要求傳入的是字串格式的引數
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 39;dict39;
程式碼如下: error_msg = "" if request.method == "POST": new_dayincome = request.POST.get("dayincome_time", None) total_bath =
statsmodels.tsa.arima_model預測時報錯TypeError: int() argument must be a string, a bytes-like object or a number, not 39;Timestamp39;
進行 時報 參數 csv 別人 間隔 get req ice 在 python 中用 statsmodels創建 ARIMA 模型進行預測時間序列: import pandas as pd import statsmodels.api as sm df = pd.read
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異常:ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.an
Python跑了一個策略,報了個異常:ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().這句換的大概意思是:“”陣列的真實值不明確“”
tensorflow程式碼執行的最後丟擲異常:TypeError: 'NoneType' object is not callable
tensorflow程式碼執行的最後丟擲異常:問題:Exception ignored in: <bound method BaseSession.__del__ of <tensorflo
Python_異常:TypeError: write() argument must be str, not list
src txt -c file 技術分享 line ffffff code err 文件寫入操作時,報錯:TypeError: write() argument must be str, not list 原因:python寫入的內容要是字符串類型的 上代碼: fp = o
動態連結警告:creating a DT_TEXTREL in object
前一段時間將一個模組做成動態庫的過程中,編譯.so的時候有一個警告:creating a DT_TEXTREL in object。 但是明明makefile裡面是有加fPIC的,不知為何還會生成DT_TEXTREL。 導致裝置啟動的時候載入.so檔案的時候,彈出了“Can