python文件读取 ,json文件的存储
with open('C:\Users\10224298\Desktop\桌面文件') as file_object:#with在不需要访问文件后将其关闭。
contents = file_object.read()
print(contents.rstrip()) rstrip可以删除每行末尾的换行符
使用jason模块保存信息:
json可以在python之间分享数据,并可以与其他程序语言一起分享。
json.dump()接受两个参数,要存储的数据和用于存储数据的文件对象。
import json
numbers = [2,3,4,5]
filename="numbers.json"
with open(filename,"w") as f_obj:
json.dump(numbers,f_obj)
json.load() 是指将列表导入内存中
import json
filename="numers.json"
with open(filename) as f_obl:
numbers = json.load(f_obj)
print (numbers)