python3 版本將list寫入txt檔案中

2021-08-28 15:18:47 字數 431 閱讀 9687

要把list型別資料寫入txt檔案中,步驟如下:

step1 :open()乙個要寫入的檔案

with open("path\filename","wb") as f:

step2 : 讀取list中的每一項

for list_mem in list:

f.write(list_mem+"\n",encoding = "utf-8")

需要注意的是一定要在write()方法中加上引數encoding,否則會報錯:error:a bytes-like object is required not str

如果是在python2.7版本下,那麼在使用write()方法時不能使用encoding = "utf-8"這種鍵值對的方式,否則報錯

typeerror: write() takes no keyword arguments。

python3版本中的urllib

urllib是python的乙個獲取url uniform resource locators,統一資源定址器 了,我們可以利用它來抓取遠端的資料進行儲存!需要注意的是 在python2版本中,有urllib2,urllib3,但是在python3中,將這兩個綜合成了乙個urllib 同時,還有幾種...

設定預設為python3版本

安裝成功以後,就可以檢視 python 的版本了 python v python2.7 python3 v python3.6.2 檢視 python 的路徑,在 usr bin 下面。可以看到 python 鏈結的是 python 2.7,所以,執行 python 就相當於執行 python 2....

python2版本程式轉python3版本

碰見有些python程式是2.7版本的,發現程式中print 函式和xrange 函式需要改變,不想乙個乙個的改,所以寫了乙個程式來更改。import os file1 r 需要更改的程式的路徑 file2 asd.txt with open file1,r encoding utf 8 as f ...