如何用Python调用系统数据文件

如何用Python调用系统数据文件

1.

  • 将数据存储到文件中:
file = open('./data/1.1.text','w',encoding = 'utf-8')
message = 'hello word'
file.write(message)
file.close()

用这个方法,可以将输入的数据存储到文件中,如果要读取,我们按照只读的方式打开文件

file = open(file ='./data/1.1.text',mode='r',encoding = 'utf-8')
info = file.read()
print(info)
file.close()

但是用这种方法的时候,存储新的数据会将原有的数据替换掉,这是我们不希望发生的,所以出现了文本文件的追加

  • 如何追加文本文件
file = open(file='./data/1.2.text',made ='a' ,encoding = 'utf-8')
message = '人说,林深时见鹿,海蓝时见鲸,夜深时见你'
file.write(mesage)
message2 = '\n但是,林深时雾起,海蓝时浪涌,夜神时梦续\n'
file.write(message2)
message3 = '\n可:鹿踏雾而来,鲸随浪而起,你未曾转身,怎知我未来'
file.write(message3)
file.close()
  • 二进制文件的操作
file = open(file='E:/WORK_IMG/lihen/a.jpg', mode='rb')
#读取计算机中的二进制文件数据,虽然图片是16进制,不过不影响使用\x开头的是16进制,\o开头的是8进制数据
print(file.read())
#将数据存储到指定位置
file2 = open(file='./data/text.jpg',mode='wb')
file2.write(file.read())
file2.close()
file.close()
  • 每次存储文件时,都会有关闭文件这个语句,太过麻烦,所以:with语法
with open('E:/WORK_IMG/lihen/a (20).JPG', 'rb') as file1:
	with open('./data/' +file1.name[file.name.rfind('/')'wb']:) as file2:
		file2.write(file1.read())
		

用这个方法可以进行代码精简
不过,随之而来的很多问题,所以出现了eval函数

2.类型转换和eval函数

数据分为基本数据类型和组合数据类型

#将程序中的字典数据,转化成字符串储存到文件中
users = {'admin':{'username': 'admin', 'password':'123','nickname':'老刘'}}
users_str = str(users)
with open('./data/2.1.text','w')as file:
	file.write(users_str)
#读取文件中的数据
with open('./data.2.1.text','r')as file:
	users = file.read()
	print(users,type(users))
	print(users.get('admin'))

这个存储方式也有问题:每个文件只能储存一个指定的数据,所以Python出现了一个特殊的模块json,可以对Python中的数据进行序列化操作

3.json的使用

with open('./data/3.1.json','w')as file:
	json.dump(users,file)
	#这里的dump是将数据存储到文件中
with open('./data/3.1.json','r')as file:
	users = json.load(file)
	print(users,type(users))
	#这里的load与上文的dump意思刚好相反,这个是读取数据

虽然已经很厉害了,不过,Python还有更好的

4.marshal的使用

#python程序将多个数据存储到文件中

# 数据准备
s = "字符串"
i = 19
f = 3.1415
b = True
c = 12 + 5j
l = [1,2,3]
d = {'username': 'admin', 'password': '123'}
x = [s, i, f, b, c, l, d]

# 存储多个数据的模块:marshal
import marshal
with open('./data/4.1.dat', 'wb') as file:
# 第一次存储一个数量:有多少个数据存储到了文件中
     marshal.dump(len(x), file)
     # 存储每个数据
     for x1 in x:
         marshal.dump(x1, file)

# 2. 将多个数据从文件中依次取出
 with open('./data/4.1.dat', 'rb') as file:
     n = marshal.load(file)

     # 将所有数据依次取出
     for x in range(n):
         print(marshal.load(file))

shelve的使用

python存取多个数据到文件中
如果按照常规方式~open/write/read方式,每个独立的数据都的单独存储 一个文件
1. str()数据:字符串:文件存储->文件读取:eval():数据
2. 数据:json.dump():文件存储->json.load():数据
如果要存储多个数据到一个文件中
3. 多个数据:总数量:marshal.dump()->marshal.load(fp):总数量:循环->marshal.load(fp)依次读取

多个数据,也可以按照比较友好的like dict的方式,将数据存储到文件中
可以将数据在文件中,通过key值直接获取对应的数据

# 数据准备
users = {'admin': {'username': 'admin', 'password': '123', 'nickname': '大牧'}}
articles = {'标题': {'title': '标题', 'content': '文章内容', 'author': users.get('admin')}}

#定义shelve
import shelve
file = shelve.open('./data/5.1')
# 1. 将多个数据按照key:value的形式存储到文件中
file['users'] = users
file['articles'] = articles
# 2. 从文件中根据key读取数据
print(file['users'], type(file['users']))

这就是python中将数据存储到文件中的基本操作…

猜你喜欢

转载自blog.csdn.net/weixin_44131901/article/details/86664646