码迷,mamicode.com
首页 > 编程语言 > 详细

json数据处理:读取文件中的json字符串,转为python字典

时间:2018-01-09 20:13:04      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:highlight   gpo   python   pos   game   with open   read   读取   conf   

方法1:

读取文件中的json字符串,

再用json.loads转为python字典

 

import json

str_file = ‘./960x540/config.json‘
with open(str_file, ‘r‘) as f:
    print("Load str file from {}".format(str_file))
    str1 = f.read()
    r = json.loads(str1)
print(type(r))
print(r)
print(r[‘under_game_score_y‘])

  

方法2:

直接用文件游标f,将json字符串连同读取和转成python字典一步完成。此时用的是josn.load(f)

 

import json

str_file = ‘./960x540/config.json‘
with open(str_file, ‘r‘) as f:
    print("Load str file from {}".format(str_file))
    r = json.load(f)
print(type(r))
print(r)
print(r[‘under_game_score_y‘])

  

 

技术分享图片

 

结论:

json模块中的loads和load的区别是:

loads是将f游标中的字符串先读取出来,在把字符串转成python字典

load是一步到位把文件游标f转成python字典。

 

json数据处理:读取文件中的json字符串,转为python字典

标签:highlight   gpo   python   pos   game   with open   read   读取   conf   

原文地址:https://www.cnblogs.com/andy9468/p/8252897.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!