自学python第九周总结

存储数据

许多程序都是要求用户输入某种信息,如让用户存储游戏首选项或提供要可视化的数据。一种简单的方法是使用模块json来存储数据。
模块json让简单的python数据结构转储到文件中,并在程序再次运行时加载该文件中的数据。话可以分享数据。

1.使用json.dump()和json.load()
使用json.dump()来存储一组数字,接受两个实参:要存储的书记以及可用于存储数据的文件对象。
json.dump(文件名,文件所在位置)
如下所示:

import json

numbers = [2,5,4,6,15,45]

filename = 'numbers.json'
with open(filename,'w') as fishc:
    json.dump(numbers,fishc)

先导入模块json,再创建一个数字列表。
并且指定了将数字列表存储的文件名。
通常将文件扩展名为.json来指出文件存储的数据为JSON格式。
然后用写入模式‘W’打开这个文件。
使用json.dump()函数将数字列表存储到上述文件中去。

如下是永json.load()函数将文件中的内容读取出来:

import json

numbers = [2,5,4,6,15,45]

filename = 'numbers.json'
with open(filename) as fishc:
    numbers = json.load(fishc)

print(numbers)

用读取的方式将文件读取出来,结果如下所示:
[2, 5, 4, 6, 15, 45]

2.保存和读取用户生成的数据
对于用户生成的数据,使用json保存有很大的益处。即使程序停止后,用户输入的信息还是存在,而不会因为程序的关闭而丢失。
如下所示:

import json

usename = input('enter your name: ')
filename = 'usename.json'
with open(filename,'w') as fishc:
    json.dump(usename,fishc)
    print('i will call you ' + usename + '!')

enter your name: kobe
i will call you kobe!

读取之前输入的文件:

import json

filename = 'usename.json'

with open(filename) as fishc:
    usename = json.load(fishc)
    print('i will call you ' + usename + '!')

输出结果为:

i will call you kobe!

3.重构
代码可以正确运行,但可以进一步的改进——将代码划分为一系列完成具体工作的函数。这就叫做重构。

import json

def get_stored_username():
    filename = 'username.json'
    try:
        with open(filename) as fishc:
            username = json.load(fishc)

    except FileNotFoundError:
        return None
    else:
        return username

def greet_user():
    username = get_stored_username()
    if username:
        print('welcome back ' + username + '!')

    else:
        username = input('what is your name?')
        filename = 'username.json'
        with open(filename,'w') as fishc:
            json.dump(username,fishc)
            print('we will remember you when you come back, ' + username +'.')

greet_user()
import json

def get_stored_username():
    filename = 'username.json'
    try:
        with open(filename) as fishc:
            username = json.load(fishc)

    except FileNotFoundError:
        return None
    else:
        return username

def get_new_username():
    username = input('enter your name: ')
    filename = 'username.json'
    with open(filename,'w') as fishc:
        json.dump(username,fishc)
    return username

def greet_user():
    username = get_stored_username()
    if username:
        print('welcome back ' + username + '!')
    else:
        username = get_new_username()
        print('we will remember you when you come back, ' + username +'.')

greet_user()

输出结果如下:

enter your name: allen
we will remember you when you come back, allen.
>>> 
=================== RESTART: H:\python\fishc\divisionn.py ===================
welcome back allen!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值