• 跟艾文学编程《Python基础》(5)Python的文件操作


    作者: 艾文,计算机硕士学位,企业内训讲师和金牌面试官,公司资深算法专家,现就职BAT一线大厂。
    邮箱: 1121025745@qq.com
    博客:https://wenjie.blog.csdn.net/
    内容:跟艾文学编程《零基础入门学Python》

    学习目标

    • 文本文件的读写
    • json 数据读写
    • csv 格式数据读取
    • 系统库sys&os 介绍

    文本文件读写

    open 函数语法:

    open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

    1. Character Meaning
    2. --------- ---------------------------------------------------------------
    3. 'r' open for reading (default)
    4. 'w' open for writing, truncating the file first
    5. 'x' create a new file and open it for writing
    6. 'a' open for writing, appending to the end of the file if it exists
    7. 'b' binary mode
    8. 't' text mode (default)
    9. '+' open a disk file for updating (reading and writing)
    10. 'U' universal newline mode (deprecated)
    11. ========= ===============================================================

    写文件

    write 语法:

    1. write(text, /) method of _io.TextIOWrapper instance
    2. Write string to stream.
    3. Returns the number of characters written (which is always equal to
    4. the length of the string).

    读文件

    read 语法:

    1. Help on built-in function read:
    2. read(size=-1, /) method of _io.TextIOWrapper instance
    3. Read at most n characters from stream.
    4. Read from underlying buffer until we have n characters or we hit EOF.
    5. If n is negative or omitted, read until EOF.

    json 数据读写

    JSON(JavaScript Object Notation) 是一种轻量级数据交互格式。

    提供两种数据格式函数: json.dumps() 和 json.loads

    • json.dump() 函数是将一个Python数据类型列表进行json格式的编码
    • json.load() 函数 将加载一个json文件格式转换字典

    json 格式写文件

    json 格式读操作

    csv 文件读写

    后续可以通过pandas 来完成

    csv 文件写操作

    csv 读文件

    系统库 sys&os 介绍

    os

    sys

    让我们一起加油!!!
  • 相关阅读:
    构造-析构函数
    Mybatis入门相关API
    Shell(2)数值运算与判断
    Web大学生网页成品HTML+CSS音乐吧 7页
    计算机毕业设计基于SpringBoot的课程在线学习平台源码+数据库 程序设计/项目定制/调试部署
    sql语法巧用之not取反
    WPF(七) Prism框架基本特性
    前端经典面试题 | v-if/v-show的原理及区别
    Java常用集合总结
    cad转shp再转3dtiles生成白模
  • 原文地址:https://blog.csdn.net/shenfuli/article/details/127943467