#程序中的数据存在文件中是否转化成字符串
#首先定义一个字典
users = {'username':'admin','password':'888','email':'123456qq.com'}
#将它转换成字符串
str_users = str(users)
#打开一个文件夹
with open('./date/3.text','w')as file:
file.write(str_users)
#再将它读取到程序中
with open('./date/3.text','r')as file2:
s = file2.read()
print(s,type(s))
#用eval吧它转换成字典
s = eval(s)
print(s,type(s))
2.json模块
import json
#准备一个字典
users = {'username':'admin','password':'234'}
#打开一个文件添加进去
with open('./date/4.text','w')as file:
json.dump(users,file)
#输出
with open('./date/4.text','r')as file2:
users = json.load(file2)
print(users,type(users))
3.marshal模块
'''
marshal模块练习
这是一个能将多个数据储存在文件中的模块
'''
import marshal
s = "字符串"
i = 3
b = True
t = (1,2)
l = [2,3]
x = [ i , b , t , l]
#打开一个文件夹
with open( './date/5.text', 'wb')as file:
marshal.dump(len(x), file)
for x1 in x:
marshal.dump(x1, file)
#取出多个数据
with open('./date/5.text','rb')as file2:
n = marshal.load(file2)
for x2 in range(n):
print(marshal.load(file2))