python将字典写入txt文件并读出来

python将字典写入txt文件并读出来,第1张

from PIL import Image
import os
import time
import cv2

s ={
    1:[1,2,3],
    2:[1,2,3],
    3:[1,2,3],
    4:[1,2,3]
}

# save txt
save_dir = './'
# get time
save_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime())
txt_path = os.path.join(save_dir, '{}.txt'.format(save_time))

# write txt
fs = open(txt_path,'w')
fs.write(str(s))
fs.close()

# read txt
fs = open(txt_path,'r+')
test_read = eval(fs.read() )
fs.close()
# 打印看下
print(test_read)

# save img
# 设置保存图像的路径
img_path = os.path.join(save_dir, '{}.jpg'.format(save_time))
# 读进来一个图像
img = cv2.imread('5.jpg')
# 转化为PIL格式 保存
Image.fromarray(img).save(img_path)

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/794959.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-06
下一篇2022-05-06

发表评论

登录后才能评论

评论列表(0条)

    保存