第十章读书笔记10.4存储数据
import json
numbers = [2,3,5,7,11,13]
filename = 'numbers.json'
with open(filename,'r+') as f_obj:
json.dump(numbers,f_obj)
import json
filename = 'numbers.json'
with open(filename) as f_obj:
numbers = json.load(f_obj)
print(numbers)
import json
username = input("What is your name? ")
filename = 'username.json'
with open(filename,'w') as f_obj:
json.dump(username,f_obj)
print("We'll remember you when you come back, " + username + "!")
import json
filename = 'username.json'
with open(filename) as f_obj:
username = json.load(f_obj)
print("Welcome back, " + username + "!")
import json
filename = 'username.json'
try:
with open(filename) as f_obj:
username = json.load(f_obj)
except FileNotFoundError:
username = input("What is your name? ")
with open(filename,'w') as f_obj:
json.dump(username,f_obj)
print("We'll remember you when you come back, " + username.title() + "!")
else:
print("Welcome back, " + username.title() + "!")
import json
def greet_user():
"""问候用户,并指出其名字"""
filename = "username.json"
try:
with open(filename) as f_obj:
username = json.load(f_obj)
except FileNotFoundError:
username = input("What is your name? ")
with open(filename,'w') as f_obj:
json.dump(username,f_obj)
print("We will remember you when you come back, " + username.title() + "!")
else:
print("Welcome back, " + username.title() + "!")
greet_user()
import json
def get_stored_username():
"""如果存储了用户名,就打印它"""
filename = 'username.json'
try:
with open(filename) as f_obj:
json.load(f_obj)
except FileNotFoundError:
return None
else:
return username
def greet_user():
"""问候用户,并指出其名字"""
username = get_stored_username()
if username:
print("Welcome back, " + username.title() +"!")
else:
username = input("What is your name?")
filename = 'username.json'
with open(filename,'w') as f_obj:
json.dump(username,f_obj)
print("We will remember you when you come back, " + username.title() + "!")
greet_user()
import json
def get_stored_username():
"""如果存储了用户名,就打印它"""
filename = 'username.json'
try:
with open(filename) as f_obj:
username = json.load(f_obj)
except FileNotFoundError:
return None
else:
return username
def get_new_username():
"""提示用户输入用户名"""
username = input("What is your name?")
filename = 'username.json'
with open(filename, 'w') as f_obj:
json.dump(username,f_obj)
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.title() + "!")
greet_user()