标签:lis tin open head rop enc model pyton 读取数据
json.load(file)
将文件中的字符串读出来,转换为json形式的列表等
[{"age": 20, "name": "tome", "sex": "male"}, {"age": 10, "name": "jerry", "sex": "female"}]
data = json.load(open(‘../datas/json_dump.json‘, ‘rt‘))
print(data)
print(type(data))
[{‘age‘: 20, ‘name‘: ‘tome‘, ‘sex‘: ‘male‘}, {‘age‘: 10, ‘name‘: ‘jerry‘, ‘sex‘: ‘female‘}]
<class ‘list‘>
json.loads(str)
将字符串转换为json形式的列表等
str1 = ‘[{"age": 20, "name": "tome", "sex": "male"}, {"age": 10, "name": "jerry", "sex": "female"}]‘
?
data = json.loads(str1)
print(data)
print(type(data))
[{‘age‘: 20, ‘name‘: ‘tome‘, ‘sex‘: ‘male‘}, {‘age‘: 10, ‘name‘: ‘jerry‘, ‘sex‘: ‘female‘}]
<class ‘list‘>
json.dump(pyton_obj)
读取数据,以字符串的形式存储在文件中
list1 = [{
‘name‘: ‘tome‘,
‘age‘: 20,
‘sex‘: ‘male‘
}, {
‘name‘: ‘jerry‘,
‘age‘: 10,
‘sex‘: ‘female‘
}]
?
data = json.dump(list1, open(‘../datas/json_dump.json‘, ‘w‘), sort_keys=True)
[{"age": 20, "name": "tome", "sex": "male"}, {"age": 10, "name": "jerry", "sex": "female"}]
json.dumps(pyton_obj)
把符合json规则的列表等转换为字符串
list1 = [{
‘name‘: ‘tome‘,
‘age‘: 20,
‘sex‘: ‘male‘
}, {
‘name‘: ‘jerry‘,
‘age‘: 10,
‘sex‘: ‘female‘
}]
?
data = json.dumps(list1, sort_keys=True)
?
print(data)
print(type(data))
[{"age": 20, "name": "tome", "sex": "male"}, {"age": 10, "name": "jerry", "sex": "female"}]
<class ‘str‘>
标签:lis tin open head rop enc model pyton 读取数据
原文地址:https://www.cnblogs.com/wanggungun/p/14791027.html