文件操作
在实际操作中,通常采集本地数据来进行数据分析,那么就需要学习Python去对本地文档进行读取与写入等操作。比如:txt文件,json文件,csv文件,excel文件等。
本地文件操作步骤
- 打开文件
- 操作文件
- 关闭文件
python操作text文件
with open(file,mode="r",encoding=None)
#file表示文件文件路径
#mode="r"表示读取文件
#mode="w"表示写入文件
#mode="a"表示追加
#encoding="utf-8"制定编码为utf-8
练习
创建 白晓吧.txt 文件并写入类容
f=open("白晓吧.txt","w",encoding="utf-8")
f.write("我是白晓吧")
f.close