分享

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

 网摘文苑 2022-11-30 发布于北京

作者: 艾文,计算机硕士学位,企业内训讲师和金牌面试官,公司资深算法专家,现就职BAT一线大厂。

内容:跟艾文学编程《零基础入门学Python》

文章图片1

学习目标

文本文件的读写

json 数据读写

csv 格式数据读取

系统库sys&os 介绍

文本文件读写

open 函数语法:

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

Character Meaning--------- ---------------------------------------------------------------'r' open for reading (default)'w' open for writing, truncating the file first'x' create a new file and open it for writing'a' open for writing, appending to the end of the file if it exists'b' binary mode't' text mode (default)'+' open a disk file for updating (reading and writing)'U' universal newline mode (deprecated)========= ===============================================================

写文件

write 语法:

write(text, /) method of _io.TextIOWrapper instance    Write string to stream.    Returns the number of characters written (which is always equal to    the length of the string).
文章图片2

读文件

read 语法:

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

json 数据读写

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

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

json.dump() 函数是将一个Python数据类型列表进行json格式的编码

json.load() 函数 将加载一个json文件格式转换字典

文章图片4

json 格式写文件

文章图片5

json 格式读操作

文章图片6

csv 文件读写

后续可以通过pandas 来完成

文章图片7
文章图片8

csv 文件写操作

文章图片9

csv 读文件

文章图片10

系统库 sys&os 介绍

os

文章图片11
文章图片12

sys

文章图片13

更多资料学习请留言。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多