数据存储 json

import json
number = [1,2,3,4]

with open('numbers.json','w'as f_obj:
    我们使用函数json.dump()将数字排列存放到文件中
    json.dump(number,f_obj)

 

 

import json
number = [1,2,3,4]

with open('number.json','w'as f_obj:
    json.dump(number,f_obj)

 

 

import json
username = raw_input('what is your name ?')
filename = 'username.json'
with open(filename,'a'as f_obj:
    json.dump(username,f_obj)
    print 'We will rember you when you come back, %s ' % username

import json

filename = "username.json"
with open(filename) as f_obj:
    username = json.load(f_obj)
    print  "Welcome back, %s" % username

 

 

 

import json
filename = "username.json"
try:
    with open(filename) as f_obj:
        username = json.load(f_obj)

except ValueError:
    username = raw_input("What is you name?")
    with open(filename,"w"as f_obj:
        json.dump(username,f_obj)
        print "We'll remember you when you come back %s" % username
#依赖与try代码块成功执行的代码都应放到else代码块中:
else:
    print "Welcome back %s" % username

 

 

 

 

[1234]

 

猜你喜欢

转载自blog.csdn.net/period000/article/details/81195142
今日推荐