寫入檔案的兩種方法

2021-10-24 21:52:49 字數 639 閱讀 2386

寫入檔案的兩種方法

方法1:file.write()

方法2:file.writelines()

write方法是將整個字串寫入到檔案中,而writelines是將乙個字串序列寫入到檔案中

如何進行選擇

如果引數是字串,就使用write方法;

如果引數是序列,就使用writelines方法。

# 開啟檔案

file =

open

('test.txt'

,'w'

)# 寫入檔案

# file.

write

("what is your name"

)seq =

file.

writelines

('\n'

.join

(seq)

)# 關閉檔案

file.

close

()

使用writelines方法的時候,我們需要進行換行,我們需要使用轉義字元\n,如果序列中每乙個元素都需要使用換行,這時候我們就可以使用 『\n』.join(seq),這樣就可以自動再每乙個元素後面新增乙個』\n』,在每乙個系統下都可以正常顯示

python 寫入csv的兩種方法

法一 開啟乙個路徑然後用csv.writer寫入 encoding utf 8 import xlrd import csv f open dataset keywords.csv w encoding utf 8 用with一樣的 with open dataset abstracts.csv w...

兩種方法獲取檔案OEP

讀取的字段都是一樣的,只是乙個直接從pe檔案中讀取,乙個對映到記憶體後再讀取 1.檔案直接訪問法 bool readoepbyfile lpctstr szfilename dword dwoep,cbread image dos header dos header sizeof image dos...

兩種方法獲取檔案OEP

讀取的字段都是一樣的,只是乙個直接從pe檔案中讀取,乙個對映到記憶體後再讀取 1.檔案直接訪問法 cpp view plain copy boolreadoepbyfile lpctstrszfilename dworddwoep,cbread image dos header dos header...