[python] 將data寫入txt

回覆文章
Lexaul
文章: 231
註冊時間: 2019-10-18, 14:28

[python] 將data寫入txt

文章 Lexaul » 2022-01-27, 14:42

參考資料: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')
[email protected]
github.com/Lexaul

Leo145x
文章: 3
註冊時間: 2023-09-20, 19:10

Re: [python] 將data寫入txt

文章 Leo145x » 2023-09-21, 04:16

代碼: 選擇全部

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

回覆文章