文件处理

文件处理

文件处理是项目开发中使数据持久化和获取配置的主要方式。我们在平时的工作中接触到的各种文件类型,比如:word,视频,txt文本文件等,Python是提供了很方便读写文件的函数,可以使得我们很方便的处理各种类型的文件。

关于文件处理主要讲解以下几方面内容:

  • I/O流
  • 文件的open/close,以及读取文件内容和写入内容到文件内
  • json序列化
  • csv文件的读取与写入

I/O流

I是input,O是output,全称是标准的输入输出数据流。

在Python中,输入就是靠input(),input()获取键盘输入,O就是靠大家熟知的print()输出打印函数

有时候我们需要格式化输出字符串和数字,主要有三种方式:

  • %格式化输出:%d是整数占位符,%f是浮点数(小数)占位符,%s是字符串占位符。
  • format():{}是占位符,与后面匹配的数量相对应,还可以写下标,例如:"{0} is {1} years old.".format("burgess",18)。
  • f-string:跟format()类似,只不过是这种形式f"...{变量名}..."。
Python 3.12.7 (main, Nov  8 2024, 17:55:36) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = input("请输入你的名字:")
请输入你的名字:burgess
>>> a
'burgess'
>>> print("HelloWorld")
HelloWorld
>>>
>>> print("%s is %d years old."%("burgess",18))
burgess is 18 years old.
>>> print("pet price is %.2f"%100.6789)
pet price is 100.68
>>> print("{} is {} years old.".format("burgess",18))
burgess is 18 years old.
>>> print("pet price is {:.2f}".format(100.6789))
pet price is 100.68
>>> price = 2.33333
>>> print(f"apple price is {price:.2f}")
apple price is 2.33
>>>

注意:实际开发中,推荐使用format()或者f-string,因为更加直观,更加见名知意。

文件的open/close,以及读取文件内容和写入内容到文件内

文件的打开与关闭涉及这两个函数,open()和close()

其中open()函数有四大模式

'r':只读模式(如果什么都不写,就是默认只读模式)

'w':写模式

'a':追加模式

'b':以二进制的形式

注意点:文件用open()打开过后要用close()关闭,否则会一直占用资源,记住跟I/O流相关的操作都会占用资源,其实在实际开发中用的是"with open()...",with的方式会自动关闭文件。

Python 3.12.7 (main, Nov  8 2024, 17:55:36) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> file = open('/etc/passwd')
>>> type(file)
<class '_io.TextIOWrapper'>
>>> file
<_io.TextIOWrapper name='/etc/passwd' mode='r' encoding='UTF-8'>
>>> file.close()
>>>
>>> with open('/etc/passwd') as f:
...     count = 0
...     for l in f:
...         count += 1
...     print(count)
...
56
>>> filename = '/root/python_code/python_2/test.txt'
>>> with open(filename) as f:
...     f.read()
...
'i love python\n\ni love java\n'
>>> file.readline()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file.
>>> file = open(filename)
>>> file.readline()
'i love python\n'
>>> file.readline()
'\n'
>>> file.readline()
'i love java\n'
>>>
>>> filename = '/root/python_code/python_2/test2.txt'
>>> with open(filename,'w') as f:
...     f.write('11111111')
...     f.write('22222222')
...
8
8
>>> with open(filename,'a') as f:
...    f.write('i love python')
...
13
>>> with open(filename) as f:
...     f.readlines()
...
['1111111122222222i love python']
>>>

test.txt

i love python

i love java

test2.txt(写入)

┌──(root㉿kali)-[~/python_code/python_2]
└─# cat test2.txt
1111111122222222

test2.txt(追加)

┌──(root㉿kali)-[~/python_code/python_2]
└─# cat test2.txt
1111111122222222i love python

json序列化

json是一种轻量级数据传输格式。

关于json的详解可以参考百度百科:

JSON_百度百科 (baidu.com)

Python 3.12.7 (main, Nov  8 2024, 17:55:36) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> courses = {1:'Linux',2:'Java',3:'Python'}
>>> json.dumps(courses)
'{"1": "Linux", "2": "Java", "3": "Python"}'
>>> with open('courses.json','w') as f:
...     f.write(json.dumps(courses))
...
42
>>>

courses.json

┌──(root㉿kali)-[~/python_code/python_2]
└─# cat courses.json
{"1": "Linux", "2": "Java", "3": "Python"} 

csv文件的读取与写入

csv是指逗号分隔值,顾名思义是用逗号将数据隔开存放,我们在拿下一个数据库对里面数据进行脱库处理,一般用的就是这个csv文件类型。

Python当中有专门处理csv文件的模块

一般在读取时转换为list类型是常用方法

Python 3.12.7 (main, Nov  8 2024, 17:55:36) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import csv
>>> with open('test.csv') as f:
...     data = list(csv.reader(f))
...
>>> type(data)
<class 'list'>
>>> for i in data:
...     print(i)
...
['1', '2', '3', '4']
['rose', 'jack', 'tom', 'xiaohong']
['12', '13', '14', '15', '11']
['160', '162', '163', '170', '153']
[]
[]
>>> with open('test2.csv','w') as f:
...     csv.writer(f).writerows(data)
...
>>>

test.csv

1,2,3,4
rose,jack,tom,xiaohong
12,13,14,15,11
160,162,163,170,153


test2.csv

┌──(root㉿kali)-[~/python_code/python_2]
└─# cat test2.csv
1,2,3,4
rose,jack,tom,xiaohong
12,13,14,15,11
160,162,163,170,153



posted on 2025-06-20 04:32  burgess0x  阅读(12)  评论(0)    收藏  举报