import json #number = [1,2,3,4,5] ipt = input('enter your name') file_name = 'number.json' with open(file_name, 'w') as file_object: json.dump(ipt, file_object) #函数json.dump()接受两个实参:要储存的数据以及可以用于存储数据的文件对象 with open(file_name, 'r') as file_object: contents = json.load(file_object) # 使用json.load()读取到内存中 print('welcome %s '%contents)
输出结果:
enter your namelilin
welcome lilin