f = open('文件路徑','文件操作方式',encoding='utf-8')
文件操作
f.close()
with open('文件路徑','文件操作方式',encoding='utf-8') as f:
文件操作
~
要用os庫import os
real_path=os.path.expanduser('~/project/xxx')
with open('text.txt',encoding='utf-8') as f:
通過f讀文件
f.readlines()
f.read()
with open('new.txt','w',encoding='utf-8') as f:
通過f寫文件
w
是覆蓋原文件;a
是向文件末尾添加新的內容
f.write("一大段文字")
f.writelines(['第一段話','第二段話','第三段話'])
注意:寫到文本中的文字不會換行,要人工輸入換行符。
import csv
with open('result.csv',encoding='utf-8') as f:
reader=csv.DictReader(f)
for row in reader:
print(row)
注意:for循環得到的row是有序字典,可以像普通字典一樣讀取。
writer.writeheader()
writer.writerows(包含字典的列表)
writer.writerow(字典)
Students who want to check the
Preface : It is said that “ Ho