1 頁 (共 1 頁)

[python] 將data寫入txt

發表於 : 2022-01-27, 14:42
Lexaul
參考資料:https://shengyu7697.github.io/python-write-text-file/

w = 寫入模式
a = 添加模式
Python open() 使用添加模式主要是可以將寫入的資料附加在原本檔案的尾巴,要使用添加模式需要在 open(filename, mode) 的第二個參數使用 'a' 來開檔,a 是 append 的意思,Python 添加模式範例如下,

代碼: 選擇全部

path = 'output.txt'
with open(path, 'w') as f:
    f.write('apple\n')
    f.write('banana\n')
    f.write('lemon\n')
    f.write('tomato\n')

Re: [python] 將data寫入txt

發表於 : 2023-09-21, 04:16
Leo145x

代碼: 選擇全部

with open("file_name","w or r",encoding="UTF-8") as f:
// Should be give encoding.