資料內(nèi)容:
1、寫文件
(1)寫入字符串數(shù)據(jù)
# coding:utf-8
# 寫入字符串數(shù)據(jù)
with open("write_example.txt", "w", encoding='utf-8') as file:
? ?file.write("Hello, World!\n")
? ?file.write("This is a new line.")
(2)寫入字節(jié)數(shù)據(jù)
使用 write() 方法將字節(jié)數(shù)據(jù)寫入文件。 可以使用 encode() 方法將字符串轉(zhuǎn)換為字節(jié)數(shù)據(jù)進行寫入。
# coding:utf-8
# 寫入字節(jié)數(shù)據(jù)
with open("write_example1.txt", "wb") as file:
? ?content = "Hello, World!\n"
? ?file.write(content.encode("utf-8"))