首页 / 技术

(15)Python的基础介绍和爬虫

2023-03-18 19:13:01技术作者:网友佚名投稿

Python 文字文件的读取和保存(下)

接下来我们会用程序实作示例来更让我们更了解文件的应用。

在程序开始之前
https://ithelp.ithome.com.tw/upload/images/20221012/20152724xn91LeYkvV.png

当程序执行之后可以发现旁边多了一个data的文件
https://ithelp.ithome.com.tw/upload/images/20221012/20152724JTFk4XIYi2.png

可以看到新的文件有我们刚刚输入的文字,代表我们的写入成功了
https://ithelp.ithome.com.tw/upload/images/20221012/20152724S8lU6NJkS4.png

如果想写入中文,可以在mode后面加上encoding= "utf-8",这样就可以写入中文了
https://ithelp.ithome.com.tw/upload/images/20221012/201527245pgBAHPLh1.png

https://ithelp.ithome.com.tw/upload/images/20221012/20152724KKuKTVJU14.png

最佳实务写法
https://ithelp.ithome.com.tw/upload/images/20221012/20152724JCfO4n9ZbK.png

将文字从文件中读取出来
https://ithelp.ithome.com.tw/upload/images/20221012/20152724AhkhaIraZ0.png

json文件中的数据
https://ithelp.ithome.com.tw/upload/images/20221012/20152724kPZ8oLdVJ9.png

使用json格式读写数据
https://ithelp.ithome.com.tw/upload/images/20221012/20152724QqgI2wHwYq.png

修改json文件的数据
https://ithelp.ithome.com.tw/upload/images/20221012/20152724bvTFOarZ6A.png

https://ithelp.ithome.com.tw/upload/images/20221012/201527246Usg0zBG5y.png

参考来源:https://www.youtube.com/watch?v=C4OkV6DrVRs&list=PL-g0fdC5RMboYEyt6QS2iLb_1m7QcgfHk&index=13


本站文章均来自互联网,仅供学习参考,如有侵犯您的版权,请邮箱联系我们删除!

猜你喜欢