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

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

时间:2023-03-17 21:06:54浏览次数:35  
标签:入门 Python 读写 编程 writing read json file open


作者: 艾文,计算机硕士学位,企业内训讲师和金牌面试官,公司资深算法专家,现就职BAT一线大厂。
 
内容:跟艾文学编程《零基础入门学Python》


学习目标

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

文本文件读写

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

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).

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

读文件

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.

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

json 数据读写

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

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

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

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

json 格式写文件

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

json 格式读操作

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

csv 文件读写

后续可以通过pandas 来完成

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

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

csv 文件写操作

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

csv 读文件

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

系统库 sys&os 介绍

os

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

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

sys

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




让我们一起加油!!!


标签:入门,Python,读写,编程,writing,read,json,file,open
From: https://blog.51cto.com/u_14361901/6128373

相关文章